2016-09-19 108 views
1

存在類似於M1,M2,M3,M4,M5,W1,W2,W3,W4,C1,C2的類的對象。其中M:人,W:女,C:兒童基於優先級重新排列陣列

它們存儲在陣列中所示:

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

現在陣中擁有基於每種類型的對象設置優先級重新排列。 Priorites給出一個枚舉:

enum Priority{ 
    One, 
    Two, 
    Three; 
} 

還要確保順序保持不變,例如:M1應該來之前M2,M2應該來之前M3等等......

輸入:Person[] arr = {M1,M3,C1,W1,W3,M2,M4,W2,C2,W4,M5}; 和優先級男子:Priority.Two 優先級女:Priority.One 優先用於兒童:Priority.Three

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

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

後者是錯誤的,因爲爲了也必須保持不變。

回答

1

鏈接下面是一個使用比較接口進行排序自定義對象的解決方案。

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; 
} 
} 
+0

我不能這樣做。這些對象以數組形式給出,您不能修改Person類。您只有Priority枚舉類包含每種類型對象的優先級。你能建議任何替代方法嗎? – RoyalTiger

+0

我已更新解決方案 – Vel

+0

謝謝@Vel ..這應該有效。我git我的答案..非常感謝你.. – RoyalTiger

1

我相信你必須爲Person類創建你自己的比較器,這樣你的Person對象可以使用Collections.sort()方法在一個集合中排序。

這裏是如何去製作自己的比較A類 http://www.tutorialspoint.com/java/java_using_comparator.htm

+0

我不認爲認爲我可以改變人類... – RoyalTiger

+0

我已經改寫了聲明。它必須「重新安排」,而不是「排序」。我認爲這個詞是誤導性的。 – RoyalTiger

2

試試下面

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())); 

輸出中

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] 

請注意:枚舉值是有序的,上面的代碼取決於值的順序枚舉類

編輯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; 
     } 
    }; 
+0

薩拉瓦納,我可以做同樣的事情,而不使用流..我的意思是說在java8之前我的解決方案是什麼 – RoyalTiger

+0

@RoyalTiger我在'Collections.sort'中添加了'Comparator'或\t'Arrays.sort' – Saravana