We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b820c86 commit 3721ed8Copy full SHA for 3721ed8
haskell/sorting-in-reverse-order.md
@@ -0,0 +1,24 @@
1
+# Sorting in reverse order
2
+
3
+`Data.Ord` contains a newtype, `Down`, which can wrap any type that has an
4
+`Ord` instance, in order to provide a reversed `Ord` instance for that type.
5
6
+For example:
7
8
+```hs
9
+Prelude> import Data.Ord (Down(..))
10
+Prelude Data.Ord> :info Down
11
+newtype Down a = Down a -- Defined in ‘Data.Ord’
12
+instance Ord a => Ord (Down a) -- Defined in ‘Data.Ord’
13
+Prelude Data.Ord> Down 3 > Down 4
14
+True
15
+```
16
17
+This can be very convenient for reversing sort order:
18
19
20
+Prelude> import Data.Ord (Down(..), comparing)
21
+Prelude Data.Ord> import Data.List (sortBy)
22
+Prelude Data.Ord Data.List> sortBy (comparing Down) [1..10]
23
+[10,9,8,7,6,5,4,3,2,1]
24
0 commit comments