0

I have a complex custom object like

public class MySortedObject {

 private static class Employee {
        public String name;
        public String ssn;
        
        public Employee(String name, String ssn) {
            super();
            this.name = name;
            this.ssn = ssn;
        }
    }

public static void main(String[] args) {
    
    Map<Integer, List<Employee>> map = new HashMap<>();
    
    Employee empl1 = new Employee("XYZ", "22DEC2019");
    Employee empl2 = new Employee("ABC", "22DEC2019");
                                                    
    Employee empl3 = new Employee("GHI", "18APR2019");
    Employee empl4 = new Employee("DEF", "18APR2019");
                                                    
    Employee empl5 = new Employee("WXY", "15OCT2019");
    Employee empl6 = new Employee("JKL", "15OCT2019");
    
    List empList1 = new ArrayList<>();
    List empList2 = new ArrayList<>();
    List empList3 = new ArrayList<>();
    
    empList1.add(empl1);
    empList1.add(empl2);
    
    empList2.add(empl3);
    empList2.add(empl4);
    
    empList3.add(empl5);
    empList3.add(empl6);
    
    map.put(1, empList1);
    map.put(2, empList2);
    map.put(3, empList3);

}

My object is a map of <Integer,List>. The list is having objects which contain a value(i.e. ssn). I want to sort my map based on this ssn value. The order of the map should be "1,3 and 2"

Anurag1804
  • 31
  • 1
  • 6
  • 1
    Shouldn't `map.put(2, empList3);` be `map.put(3, empList3);`? Also can you explain what you mean by "*order of the map should be "map.get(1)", then "map.get(3)" and at last "map.get(2)" i.e. "1,3 and 2"*"? – Pshemo Sep 20 '20 at 18:15
  • I don't understand your question. – Unmitigated Sep 20 '20 at 18:18
  • Sorry that was a typo. Updated the code. I wanted to mention the order of the objects i.e. 1,3,2 – Anurag1804 Sep 20 '20 at 18:19
  • @iota, I want to sort my map on the basis of the "ssn" field of employee object – Anurag1804 Sep 20 '20 at 18:20
  • `ssn` of all the element of list is same ? – Eklavya Sep 20 '20 at 18:24
  • @Rono Yes...for a single list it is same. It is different w.r.t other list – Anurag1804 Sep 20 '20 at 18:28
  • Does `ssn` has to be as `String` or can we assume that it is some more specific date related type like `LocalDate`? – Pshemo Sep 20 '20 at 18:48
  • *"My object is a map of "* No, your object is a map of – Andreas Sep 20 '20 at 18:57
  • @Andreas. Sorry for the typo. Updated the problem statement – Anurag1804 Sep 20 '20 at 18:59
  • Use LocalDate for ssn field then it will be easy to sort and collect as LinkedHashMap. Like `Map> res = map.entrySet().stream().sorted((a, b) -> b.getValue().get(0).ssn.compareTo(a.getValue().get(0).ssn)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (a, b) -> a, LinkedHashMap::new)); ` [Demo](https://onlinegdb.com/rJped7rSw) – Eklavya Sep 20 '20 at 19:02
  • `map = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getValue().get(0).getSsn())).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a,b)->a, LinkedHashMap::new));` – Andreas Sep 20 '20 at 19:05
  • `SimpleDateFormat formatter = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH); String dateInString = ssn; Date date=null; try { date = formatter.parse(dateInString); } catch (ParseException e) { e.printStackTrace(); }` _--date sort function_ – Priya Sep 21 '20 at 08:23
  • `Collections.sort(list, (Map.Entry> o1, Map.Entry> o2)-> { try { Date d1= stringToDate(o1.getValue().get(0).ssn); Date d2= stringToDate(o2.getValue().get(0).ssn); if(d1.before(d2)) { return 1; } else if(d2.before(d1)) { return -1; } } catch (ParseException e) { } return 0; });` Compare function, please do map to linkedlist conversion before this compare fun & list to linkedhashmap conversion after compare fun. You can get your expected output – Priya Sep 21 '20 at 08:36

0 Answers0