sorting - Sort a list of tuples by their second element and absolute value -



sorting - Sort a list of tuples by their second element and absolute value -

how sort list

[("apple",3),("apple",-2),("pear",1)]

by sec element in tuple, , absolute value,

[("pear",1),("apple",-2),("apple",3)]

here's paraphrased approach:

import data.list import ghc.exts sortwith (abs . snd) [("apple",3),("apple",-2),("pear",1)]

you can apply problem requiring sort container transformed keys.

in case abs . snd function used transforming ("foo", -5) 5 , sorted new keys.

sorting haskell tuples

Comments

Popular posts from this blog

java - Bypassing "final local variable defined in an enclosing type" -

c - Compilation of a code: unkown type name string -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -