This is quite a bit more involved thanks to the structure of Aeson. I'll post the code without much explanation, other than "sortBy
and comparing
are your friends":
import Data.Aeson.Types (parseMaybe, Value, Parser)
import Data.Ord (comparing)
import Data.List (sortBy)
import Data.Scientific (Scientific)
import Data.Text (Text)
sortByKeyForNumberField :: Text -> [Value] -> [Value]
sortByKeyForNumberField key = sortBy (comparing $ parseMaybe parserFunction)
where
parserFunction :: Value -> Parser Scientific
parserFunction = withObject "some object" (.: key)
You can then calculate your desired list by evaluating sortByKeyForNumberField "delta" lists
.
However, you're much better off parsing Aeson's AST to a meaningful data type (which you'll have to define yourself, because you know the application) and implementing sorting for it. Aeson is really made as an AST for parsing and rendering JSON, and not for manipulating JSON.