5 ms·
>That's not necessary. You can just create a type that embeds the type you're sorting But that's more work (and more boilerplate) than simply passing an anonym
by chongli 13y ago
>That's not necessary. You can just create a type that embeds the type you're sorting
But that's more work (and more boilerplate) than simply passing an anonymous comparator to the sort function.
- drivebyacct2 13y agoWhich isn't possible at the language level, so why are we even discussing impl with jlujan's shitty attitude.
- cmccabe 13y agoLet me guess. You are coming from Java. In Java this would be: myArray.sort(new Comparator<MyClass> ({ @Override CompareTo(MyClass lhs, MyClass rhs) { return rhs.compare(lhs); } })); Is that really shorter? I don't think so. In any case, there is a more elegant solution in Go, which is simply: sort.Sort(sort.Reverse(array)) You need to open your mind a little bit, to learn a new language.
- chongli 13y agoYou are coming from Java. Nope. Clojure and Haskell. Clojure: user=> (sort > (vals {:foo 5, :bar 2, :baz 10})) (10 5 2) Haskell: >>> sortBy (flip compare) . map snd $ [("foo", 5), ("bar", 2), ("baz", 10)] [10,5,2]