java - Custom sorting - -


we have unordered set of values below ex:

name   cost  ---------    - - - - -  bbb      100-500 ccc       500+more aaa       1-100 

we wanted display above values in below format(orderby cost).

name   cost  ---------    - - - - -  aaa       1-100 bbb      100-500 ccc       500+more 

is there possible way custom sorting using java or best way sort.

please me on this.

java has called comparator, used sort things.

in in comparator can define properties used sorting. example, sort cost can use following example. depending on types of properties can use compareto or otherwise write own sorting logic (see comparator docs).

class mycomparator implements java.util.comparator<myobject> {   @override   public int compare(myobject o1, myobject o2) {     return o1.cost.compareto(o2.cost);   } } list<myobject> items = new arraylist<>(); items.sort(new mycomparator());  // or use lambda instead items.sort((o1, o2) -> o1.cost.compareto(o2.cost)); 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -