RoyalTiger RoyalTiger - 1 year ago 101
Java Question

Rearrange the array based on priority

There are objects of

class like: M1,M2,M3,M4,M5,W1,W2,W3,W4,C1,C2. Where M: Man, W: Woman, C: Child

They are stored in the array below:

Person[] arr = {M1,M3,C1,W1,W3,M2,M4,W2,C2,W4,M5};

Now the array has to be rearranged based on a priority set for each type of objects. Priorites are given in a Enum:

enum Priority{

Also make sure that the order remain same, for example: M1 should come before M2, M2 should come before M3 and so on...

Person[] arr = {M1,M3,C1,W1,W3,M2,M4,W2,C2,W4,M5};

and Priority for Man: Priority.Two
Priority for Woman: Priority.One
Priority for Child: Priority.Three

Expected Output:
Person[] arr = {W1,W2,W3,W4,M1,M2,M3,M4,M5,C1,C2};

Incorrect Output:
Person[] arr = {W1,W3,W2,W4,M1,M5,M4,M3,M2,C2,C1};

The latter is wrong, since order must also remain same.

Answer Source

Try below

    final List<Person> persons = new ArrayList<>();
    IntStream.rangeClosed(1, 5).mapToObj(i -> new Person("M" + i, Priority.TWO)).forEach(persons::add);
    IntStream.rangeClosed(1, 4).mapToObj(i -> new Person("W" + i, Priority.ONE)).forEach(persons::add);
    IntStream.rangeClosed(1, 2).mapToObj(i -> new Person("C" + i, Priority.THREE)).forEach(persons::add);
    persons.add(new Person("M11", Priority.TWO)); // test to sort by number
    List<Person> sorted =
            .sorted(Comparator.comparing(Person::getPriority).thenComparingInt(p -> Integer.parseInt(p.getName().substring(1))))
    System.out.println("Before sort " +;
    System.out.println("After sort " +;


Before sort [M1, M2, M3, M4, M5, W1, W2, W3, W4, C1, C2, M11]
After sort [W1, W2, W3, W4, M1, M2, M3, M4, M5, M11, C1, C2]

please note: The values in enum are ordered, above code depends on the order of values in enum class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download