2016-11-12 158 views
0

嗯,我有一個類命名的數據:基於身份和頭銜的java機器人分揀兩次

public class Data implements Comparable<Data>{ 
    public String title; 
    public String description; 
    public String inststatus; 
    public int imageId; 

    Data(String title, String description, int imageId,String inststatus) { 
     this.title = title; 
     this.description = description; 
     this.imageId = imageId; 
     this.inststatus = inststatus; 
    } 

    @Override 
    public int compareTo(Data another) { 

     if(this.inststatus.equals("Present")){ 
      return this.title.compareTo(another.title); 
     }else // Not Present 
     return this.inststatus.compareTo(another.inststatus); // ascending 


    } 

} 

現在,如果你看到基於按升序排列標題我已經比較它的類,但我想要做的排序兩個規則

inststatus有2個值: - 「存在」和「不存在」 1. inststatus =「存在」應該來在上面,在此之後,所有的人都應該以升序排序 2. inststatus =「不在場」應該出現在底部,之後,他們都應該按升序排列

現在讓我們來看一個例子,我們有價值觀

title = abc ,status = present 
title = bcd , status not present 
title = xyz ,status = present 
title = aac ,status not present 

現在上面2條規則後,它應該是列表排序

1. abc // present and also ascending 
2. xyz 
3. aac // now left ones not present and also ascending 
4. bcd 

回答

0

所以後,inststatus更重要的則標題。是對的嗎?如果是的話,首先你應該檢查inststatus,如果他們的inststatus是一樣的,那麼你應該檢查標題。如果inststatus不相同,並且如果您的對象存在,則意味着其他人不存在,因此您可以返回1.其餘部分相同。如果它不存在,則返回-1。

你可以按如下方式實現。

public int compareTo(Data another){ 

    if(inststatus.equals(another.inststatus)){ 
     return title.compareTo(another.title); 
    } 
    return inststatus == "present" ? 1: -1; 

} 
+0

完美標記爲答案,按預期工作 – AndroidBeginnerJhon