How to use Comparator in Java to sort

I learned how to use the comparable but I’m having difficulty with the Comparator. I am having a error in my code: Exception in thread “main” java.lang.ClassCastException: New.People cannot be cast to java.lang.Comparable at java.util.Arrays.mergeSort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at java.util.Collections.sort(Unknown Source) at New.TestPeople.main( Here is my code: import java.util.Comparator; public class People implements Comparator … Read more

lodash multi-column sortBy

There’s a nifty method to sort an array of objects based on several properties: var data = _.sortBy(array_of_objects, [‘type’, ‘name’]); However that is only for ascending sorting. Is there some handy way of defining direction per column? E.g. var data = _.sortBy(array_of_objects, [{‘type’: ‘asc’}, {‘name’: ‘desc’}]); 7 Answers 7

What is the shortest way to simply sort an array of structs by (arbitrary) field names?

I just had a problem where I had an array of structs, e.g. package main import “log” type Planet struct { Name string `json:”name”` Aphelion float64 `json:”aphelion”` // in million km Perihelion float64 `json:”perihelion”` // in million km Axis int64 `json:”Axis”` // in km Radius float64 `json:”radius”` } func main() { var mars = new(Planet) … Read more


In short: Is there any way to sort the values in a GROUP_CONCAT statement? Query: GROUP_CONCAT((SELECT GROUP_CONCAT( SEPARATOR ” &raquo; “) FROM test_competence AS node, test_competence AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND = l.competence AND != 1 ORDER BY parent.lft) SEPARATOR “<br />\n”) AS competences I get this row: Crafts … Read more

Are there any worse sorting algorithms than Bogosort (a.k.a Monkey Sort)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 7 years ago. Improve this question My co-workers took me back in time to my University days with a discussion of … Read more

Sorting a tab delimited file

I have a data with the following format: foo<tab>1.00<space>1.33<space>2.00<tab>3 Now I tried to sort the file based on the last field decreasingly. I tried the following commands but it wasn’t sorted as we expected. $ sort -k3nr file.txt # apparently this sort by space as delimiter $ sort -t”\t” -k3nr file.txt sort: multi-character tab `\\t’ … Read more