简体   繁体   中英

Alphanumeric sorting for list of objects in java

Please help me out to sort the list of objects given below

Input: List list = [ob1,ob2,ob3,ob4] 
        ob1 = ["BH11", "val2", "val3"];
        ob2 = ["BH1", "val4", "val5"];
        ob3 = ["BH12", "val6", "val7"];
        ob4 = ["BH2", "val8", "val9"];

After sorting based upon values at index 0 of each object ie BH11, BH1 etc. Expected output : sortedList = [ob2,ob4,ob1,ob3]

尝试使用

Collection.sort(list, (a,b)->{return a[0].compareTo(b[0])});

Implement the Comparable interface in the object class and override the compareTo() method to your liking. You can then use sort

See java documentation here: https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM