Sorted List in Java -


i need sort list in java below:

list contains collection of objects this,

list list1 = {obj1, obj2,obj3,.....}; 

i need final list has "lowest value" , "repetition of name should avoid".

ex:

list list1 = {[nellai,10],[gujarath,10],[delhi,30],[nellai,5],[gujarath,15],[delhi,20]} 

after sorting , need list :

list list1 = {[nellai,5],[gujarath,10],[delhi,20]}; 

i have 2 delhi (30,20) in list. need 1 delhi has lowest fare (20).

how in java?

gnaniyar zubair

almost same @visage answer, order different:

public class namefare {     private string name;     private int fare;     public string getname() {         return name;     }     public int getfare() {         return fare;     }     @override public void equals(object o) {         if (o == this) {             return true;         } else if (o != null) {             if (getname() != null) {                 return getname().equals(o.getname());             } else {                 return o.getname() == null;             }         }         return false;     } } .... public collection<namefare> sortandmerge(collection<namefare> tosort) {     arraylist<namefare> sorted = new arraylist<namefare>(tosort.size());     (namefare nf : tosort) {         int idx = sorted.getindexof(nf);          if (idx != -1) {             namefare old = sorted.get(idx);             if (nf.getfare() < old.getfare()) {                 sorted.remove(idx);                 sorted.add(nf);             }         }     }     collections.sort(sorted, new comparator<namefare>() {         public int compare(namefare o1, namefare o2) {             if (o1 == o2) {                 return 0;             } else {                 if (o1.getname() != null) {                     return o1.getname().compareto(o2.getname());                  } else if (o2.getname() != null) {                     return o2.getname().compareto(o1.getname());                  } else {                     return 0;                 }             }         }     }); } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -