RoyalTiger
RoyalTiger

Reputation: 541

Rearrange the array based on priority

There are objects of Person 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{
    One,
    Two,
    Three;
}

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

Input: 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.

Upvotes: 1

Views: 2703

Answers (3)

Vel
Vel

Reputation: 787

Here is the solution using Comparator interface for sorting custom objects.

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Person {

private int priority;
private String objName;

Person(String name,int value){
    this.objName = name;
    this.priority = value;
}

public int getPriority() {
    return priority;
}

public void setPriority(int priority) {
    this.priority = priority;
}

public String getObjName() {
    return objName;
}

public void setObjName(String objName) {
    this.objName = objName;
}   

public static void main(String[] args) {
    Person M1 = new Person("M1",Priority.valueOf("ONE").getValue());
    Person M2 = new Person("M2",Priority.valueOf("ONE").getValue());
    Person M3 = new Person("M3",Priority.valueOf("ONE").getValue());
    Person M4 = new Person("M4",Priority.valueOf("ONE").getValue());
    Person M5 = new Person("M5",Priority.valueOf("ONE").getValue());
    Person W1 = new Person("W1",Priority.valueOf("THREE").getValue());
    Person W2 = new Person("W2",Priority.valueOf("THREE").getValue());
    Person W3 = new Person("W3",Priority.valueOf("THREE").getValue());
    Person W4 = new Person("W4",Priority.valueOf("THREE").getValue());      
    Person C1 = new Person("C1",Priority.valueOf("TWO").getValue());
    Person C2 = new Person("C2",Priority.valueOf("TWO").getValue());

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

    List<Person> list = Arrays.asList(arr);

    System.out.println("Before sort...");
    for(Person p : list){
        System.out.println(p.getObjName());
    }

    Collections.sort(list, new PersonComparator<Person>());

    System.out.println("After sort...");
    for(Person p : list){
        System.out.println(p.getObjName());
    }
}

}

enum Priority{  
ONE(1),TWO(2),THREE(3); 

private int value;  
public int getValue() {
    return value;
}   
Priority(int v){
    this.value = v;
}
}

class PersonComparator<T> implements Comparator<Person> {
public int compare(Person p1, Person p2) {
    //Sorting based on priority
    int v1 = p1.getPriority();
    int v2 = p2.getPriority();
    ;
    if (v1 - v2 == 0) {
        //Sorting based on object name
        int i1 = Integer.parseInt(p1.getObjName().substring(1, 2));
        int i2 = Integer.parseInt(p2.getObjName().substring(1, 2));
        return i1 - i2;
    }
    return v1 - v2;
}
}

Upvotes: 1

Saravana
Saravana

Reputation: 12817

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 = persons.stream()
            .sorted(Comparator.comparing(Person::getPriority).thenComparingInt(p -> Integer.parseInt(p.getName().substring(1))))
            .collect(Collectors.toList());
    System.out.println("Before sort " + persons.stream().map(Person::getName).collect(Collectors.toList()));
    System.out.println("After sort " + sorted.stream().map(Person::getName).collect(Collectors.toList()));

ouput

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

Edit-1

Compartor

    Comparator<Person> comp = new Comparator<Person>() {
        @Override
        public int compare(Person p1, Person p2) {
            int co = p1.getPriority().compareTo(p2.getPriority());
            if (co == 0)
                return Integer.parseInt(p1.getName().substring(1)) - Integer.parseInt(p2.getName().substring(1));
            return co;
        }
    };

Upvotes: 2

RAZ_Muh_Taz
RAZ_Muh_Taz

Reputation: 4089

I believe you have to create your own comparator for the Person Class so your Person objects can be sorted in a collection using the Collections.sort() method.

Here is a link on how to go about making your own comparator for a Class http://www.tutorialspoint.com/java/java_using_comparator.htm

Upvotes: 1

Related Questions