0

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 i.e. BH11, BH1 etc. Expected output : sortedList = [ob2,ob4,ob1,ob3]

1

2 Answers 2

1

Try to use

Collection.sort(list, (a,b)->{return a[0].compareTo(b[0])});
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.