2016-02-28 98 views
0

我正在試着做一個函數,它允許你以sort的日期按降序或升序排列。但是,我不太確定我應該如何去做這件事。這是我到目前爲止已經制定出來,卻得到了完全被卡住:排序ArrayList?

public void sortByYear() { 
    String askID = JOptionPane.showInputDialog(null, "Would you like to sort by ascending or descending?"); 
    if(askID.equalsIgnoreCase("ascending")){ 
     for(String datePersons : personList){ 
     String[] splitPerson = datePersons.split("_"); 
     String date = splitPerson[5]; 
     } 
    } else if(askID.equalsIgnoreCase("descending")){ 

    } 
} 

的格式,我ArrayList作品有,是這樣的:ID_NAME_MAIL_PHONE_CITY_DATETIME#

其中在ArrayList,在它的數據,看起來像這樣:

7_Geoffrey [email protected]_1-382-295-5799_Warwick_2010-03-27 09:47:41 

如果我有99個不同日期的不同人,我將如何能夠排序他們?我用這部分拆分和找到日期:

String[] splitPerson = datePersons.split("_"); 
String date = splitPerson[5]; 

最好的問候。

+0

看這個:http://stackoverflow.com/questions/15085608/how-to-sort-an-arraylist-of-date-in-ascending-and-descending-order-which-is-in-s – developer033

+0

你的例子'dataPersons'中的真實用戶數據? :-O –

+0

從我的老師編寫userdata。他給了我們99行用戶數據,但是,在過去的3個小時裏,我一直被困在最後一個問題上。 –

回答

2

使用Arrays.sort方法並使用自定義比較器。在比較器實現中,解析日期字符串,構造日期對象並進行比較。

Arrays.sort(personList, new Comparator<String>() { 
    public int compare(String person1, String person2) { 
     Date date1 = parseDate(person1); 
     Date date2 = parseDate(person2); 
     return date1.compareTo(date2); 
     //return date2.compareTo(date1) for reverse order 
    } 
}); 

如果personList是列表/收藏而不是在Collections類的陣列,使用的排序方法,其工作原理類似於Arrays.sort方法。

+0

對不起,如果這可能太簡單,但date1和date2從哪裏來?這會自動貫穿所有99行的人,而不是2? –

+0

您必須實施parseDate方法。是的,它會運行儘可能多的比較,以正確排序列表或數組。 parseDate方法將如下所示: private Date parseDate(String person) { String[] splitPerson = person.split("_"); String date = splitPerson[5]; return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date); }

0

首先,使用例如分析日期字符串到合適的java.util.Date對象。一個java.text.SimpleDateFormat(假設你的日期格式一致)。然後,您可以使用自然排序或反向排序對Date列表進行排序,具體取決於askID的值。

請注意,由於反向比較器出現在外,因此不需要寫入自定義比較器:java.util.Collections.reverseOrder()。以下是一個例子片段:

public static void sortByYear() throws ParseException { 
    // ... 
    DateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 
    List<Date> dateList = new ArrayList<>(); 
    for (String date : personList) { 
     Date parsedDate = format.parse(date); 
     dateList.add(parsedDate); 
    } 
    if (askID.equalsIgnoreCase("ascending")) { 
     Collections.sort(dateList); 
    } else { 
     Collections.sort(dateList, Collections.reverseOrder()); 
    } 
} 
0

面向對象的解決問題的方法是創建一個模型類,Person,用於保持當前連接成一個字符串的不同的屬性。然後,您可以使用Comparable接口(例如,像這樣:

public class Person implements Comparable<Person>{ 

    private Date dateTime; 

    /* 
    * Clip other fields, constructor and accessor methods. 
    */ 

    @Override 
    public int compareTo(Person other) { 
     return this.dateTime.compareTo(other.dateTime); 
    } 
} 

現在一個Person的排序將基於所述dateTime屬性,分選以升序List<Person> myPersonList日期時間順序被定義爲是由Collections.sort(myPersonList)降序排列那樣簡單Collections.sort(myPersonList, Collections.reverseOrder())一樣簡單和排序。

另外,快速和骯髒的解決問題的方法是創建一個自定義Comparator,這將允許您訂購基礎上的日期時間標記的List<String> myStringList列表(在指數5,當由_分隔符分割)每個元素的:

public class UserInfoDateTimeComparator implements Comparator<String> { 

    // see docs for correct format: 
    // https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 
    private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat(...) 

    @Override 
    public int compare(String s1, String s2) { 

     // guards for null parameters 
     if (s1 == null) { 
      return s2 == null ? 0 : -1; 
     } else if (s2 == null) { 
      return 1; 
     } 

     try { 
      Date dateTime1 = DATETIME_FORMAT.parse(s1.split("_")[5]); 
      Date dateTime2 = DATETIME_FORMAT.parse(s2.split("_")[5]); 
      return dateTime1.compareTo(dateTime2); 
     } catch (ParseException e) { 
      throw new IllegalArgumentException(e); 
     } 
    } 
} 

隨着Comparator<String> myComparator = new UserInfoDateTimeComparator()myStringList可以通過Collections.sort(myStringList, myComparator)升序日期時間順序存儲,並通過Collections.sort(myStringList, Collections.reverseOrder(myComparator))降序排列。