2013-10-14 20 views
0

我想對包含屬性名稱和課程的學生對象列表進行排序,以便根據名稱完成排序,如果兩個名稱相同,則應考慮排序過程。我可以單獨做,而是希望有一個單一的列表... plz幫助...如何使用java中的不同屬性對列表進行排序

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package studentlist; 

import java.util.*; 

/** 
* 
* @author Administrator 
*/ 
public class StudentList { 

    /** 
    * @param args the command line arguments 
    */ 
    String name, course; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getCourse() { 
     return course; 
    } 

    public void setCourse(String course) { 
     this.course = course; 
    } 

    public StudentList(String name, String course) { 
     this.name = name; 
     this.course = course; 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     List<StudentList> list = new ArrayList<StudentList>(); 

     list.add(new StudentList("Shaggy", "mca")); 
     list.add(new StudentList("Roger", "mba")); 
     list.add(new StudentList("Roger", "bba")); 
     list.add(new StudentList("Tommy", "ca")); 
     list.add(new StudentList("Tammy", "bca")); 

     Collections.sort(list, new NameComparator()); 
     Iterator ir = list.iterator(); 

     while (ir.hasNext()) { 
      StudentList s = (StudentList) ir.next(); 
      System.out.println(s.name + " " + s.course); 
     } 

     System.out.println("\n\n\n "); 
     Collections.sort(list, new CourseComparator()); 
     Iterator ir1 = list.iterator(); 

     while (ir1.hasNext()) { 
      StudentList s = (StudentList) ir1.next(); 
      System.out.println(s.name + " " + s.course); 
     } 

    } 

} 

class NameComparator implements Comparator<StudentList> { 

    public int compare(StudentList s1, StudentList s2) { 

     return s1.name.compareTo(s2.name); 

    } 

} 

class CourseComparator implements Comparator<StudentList> { 

    public int compare(StudentList s1, StudentList s2) { 
     return s1.course.compareTo(s2.course); 
    } 
} 
+0

你已經知道條件,現在將其應用於比較級 –

回答

3

一個簡單的方法:

排序根據名字第一次名單。之後再次循環列表。如果您依次找到相同的名稱,則比較課程並相應地更新列表。

+0

這基本上是做兩次排序,這是沒有必要的 –

+0

@Mateusz是的。我只是提出了一個簡單的方法,當我回答這個問題時我想到了。雖然它是低效的,但它畢竟正確地回答這個問題:) – CodingBird

6

你只需要將這兩個比較放在一個比較器

  • 首先測試name相等或不:
    • 如果它們不相等,則返回的name比較的結果。
    • 如果他們是平等的,移動到比較courses

以下比較將工作:

class NameCourseComparator implements Comparator<StudentList> { 

    public int compare(StudentList s1, StudentList s2) { 
     if (s1.name.equals(s2.name)) { 
      return s1.course.compareTo(s2.course); 
     } 
     return s1.name.compareTo(s2.name); 
    } 
} 
+0

其給出輸出:羅傑BBA 湯米BCA 湯米CA 羅傑MBA 長毛馬華我希望它是:羅傑BBA羅傑MBA 湯米BCA 湯米CA 毛茸茸的mca –

+0

@TanujJeena你如何使用它?在我的情況下工作正常。你還在排序兩次嗎?你現在只需要一種新的NameCourseComparator()'。 –

0

你可以僅僅調整了NameComparator有點

class NameComparator implements Comparator<StudentList> { 
     public int compare(StudentList s1, StudentList s2) { 
     if(s1.getName().equals(s2.getName()) { 
      return s1.getCourse().compareToIgnoreCase(s2.getCourse()); 
     }else { 
      return s1.getName().compareToIgnoreCase(s2.getName()); 
     } 
} 
+0

你應該使用等於比較字符串 – upog

+0

@upog:感謝您的更正。我相應地更新了我的答案。 – VikramV

+0

**感謝傢伙.....現在整理..... ** –

相關問題