简体   繁体   中英

Sorting a List<Object> based on a String array inside the object

I have a list of objects that needs to be sorted based on a String variable inside. The problem is that this string is in csv style so I need to split it and then sort it. The string contains days of the week and the whole list needs to sorted considering today being the first and then subsequent days being the next to show up. I have implemented the code I have pasted below and it works but I am not sure if its the most efficient way of doing it. Your help is much appreciated.

public class Student {
    private String studentDays;
    private String name;
    private int sortPriority;

    public String getStudentDays() {
        return studentDays;
    }

    public void setStudentDays(String studentDays) {
        this.studentDays = studentDays;
    }

    public int getSortPriority() {
        return sortPriority;
    }

    public void setSortPriority(int sortPriority) {
        this.sortPriority = sortPriority;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And this is the sort I have implemented

public class SortNew {
    private List<Student> students;
    private HashMap<String, Integer> dayPriorityMap = new HashMap<String, Integer>();

    public SortNew(List<Student> students) {
        this.students = students;
        generateDayMap();
    }

    public void sort() {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentDays() != null) {
                List<String> studentBatchDays = Arrays.asList(students.get(i).getStudentDays().split(","));
                if (studentBatchDays.contains("Mon")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Mon"));
                } else if (studentBatchDays.contains("Tue")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Tue"));
                } else if (studentBatchDays.contains("Wed")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Wed"));
                } else if (studentBatchDays.contains("Thu")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Thu"));
                } else if (studentBatchDays.contains("Fri")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Fri"));
                } else if (studentBatchDays.contains("Sat")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Sat"));
                } else if (studentBatchDays.contains("Sun")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Sun"));
                }
            }
        }

        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Integer.compare(o1.getSortPriority(), o2.getSortPriority());
            }

        });

        for (Student student : students) {
            System.out.println(student.getName());
        }
        System.out.println(dayPriorityMap.toString());
    }

    public void generateDayMap() {
        Calendar cal = Calendar.getInstance();
        for (int i = 1; i <= 7; i++) {
            if (i > 1) {
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
            int t = cal.get(Calendar.DAY_OF_WEEK);
            String day = getDay(t);
            dayPriorityMap.put(day, i);
        }
    }

    public String getDay(int day) {
        switch (day) {
        case Calendar.SUNDAY:
            return "Sun";
        case Calendar.MONDAY:
            return "Mon";
        case Calendar.TUESDAY:
            return "Tue";
        case Calendar.WEDNESDAY:
            return "Wed";
        case Calendar.THURSDAY:
            return "Thu";
        case Calendar.FRIDAY:
            return "Fri";
        case Calendar.SATURDAY:
            return "Sat";
        }
        return null;
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();

        Student student = new Student();
        student.setName("One");
        student.setSortPriority(10);
        student.setStudentDays("Sun,Tue,Wed");
        students.add(student);

        Student student1 = new Student();
        student1.setName("Two");
        student1.setSortPriority(10);
        student1.setStudentDays("Sat,Thu,Sun");
        students.add(student1);

        Student student2 = new Student();
        student2.setName("Three");
        student2.setSortPriority(10);
        students.add(student2);

        Student student3 = new Student();
        student3.setName("Four");
        student3.setSortPriority(10);
        student3.setStudentDays("Fri,Sun,Sat");
        students.add(student3);

        SortNew s = new SortNew(students);
        s.sort();
    }
}

You could try to use Java 8 functional style features in order to avoid repetitive coding patterns:

if (studentBatchDays.contains("Mon")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Mon"));
} else if (studentBatchDays.contains("Tue")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Tue"));
} else if (studentBatchDays.contains("Wed")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Wed"));
} else if (studentBatchDays.contains("Thu")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Thu"));
} else if (studentBatchDays.contains("Fri")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Fri"));
} else if (studentBatchDays.contains("Sat")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Sat"));
} else if (studentBatchDays.contains("Sun")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Sun"));
}

You could put a consumer instead of values into the dayPriorityMap . And then iterate through the weekdays and use the consumer for each week day.

Something along these lines:

import java.util.*;
import java.util.function.Consumer;

public class SortNew {
    private List<Student> students;
    private Map<String, Consumer<Integer>> dayPriorityMap = new HashMap<>();

    public SortNew(List<Student> students) {
        this.students = students;
        generateDayMap();
    }

    public void sort() {
        List<String> weekDays = Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentDays() != null) {
                List<String> studentBatchDays = Arrays.asList(students.get(i).getStudentDays().split(","));
                final int studentIndex = i;
                weekDays.stream().filter(studentBatchDays::contains)
                        .findFirst()
                        .ifPresent(weekday -> dayPriorityMap.get(weekday).accept(studentIndex));
            }
        }

        students.sort(Comparator.comparingInt(Student::getSortPriority));

        for (Student student : students) {
            System.out.println(student.getName());
        }
    }

    public void generateDayMap() {
        Calendar cal = Calendar.getInstance();
        for (int i = 1; i <= 7; i++) {
            if (i > 1) {
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
            int t = cal.get(Calendar.DAY_OF_WEEK);
            String day = getDay(t);
            final int sortPrio = i;
            dayPriorityMap.put(day, (in) -> students.get(in).setSortPriority(sortPrio));
        }
    }

    private String getDay(int day) {
        switch (day) {
            case Calendar.SUNDAY:
                return "Sun";
            case Calendar.MONDAY:
                return "Mon";
            case Calendar.TUESDAY:
                return "Tue";
            case Calendar.WEDNESDAY:
                return "Wed";
            case Calendar.THURSDAY:
                return "Thu";
            case Calendar.FRIDAY:
                return "Fri";
            case Calendar.SATURDAY:
                return "Sat";
        }
        return null;
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();

        Student student = new Student();
        student.setName("One");
        student.setSortPriority(10);
        student.setStudentDays("Sun,Tue,Wed");
        students.add(student);

        Student student1 = new Student();
        student1.setName("Two");
        student1.setSortPriority(10);
        student1.setStudentDays("Sat,Thu,Sun");
        students.add(student1);

        Student student2 = new Student();
        student2.setName("Three");
        student2.setSortPriority(10);
        students.add(student2);

        Student student3 = new Student();
        student3.setName("Four");
        student3.setSortPriority(10);
        student3.setStudentDays("Fri,Sun,Sat");
        students.add(student3);

        SortNew s = new SortNew(students);
        s.sort();
    }
}

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