2012-02-02 11 views
67

ऑब्जेक्ट के भीतर किसी विशेष मान द्वारा ऑब्जेक्ट्स की एक सरणी सूची को सॉर्ट करने का प्रयास कर रहा है। ऐसी चीज करने का सबसे अच्छा तरीका क्या होगा। क्या मुझे कुछ प्रकार के तुलनित्र के साथ Collections.sort() का उपयोग करना चाहिए?एंड्रॉइड-जावा- ऑब्जेक्ट

मैं ऑब्जेक्ट्स की एक सूची को एक फ्लोट वैल्यू द्वारा क्रमबद्ध करने की कोशिश कर रहा हूं, जिसमें वे एक चर में हैं।

संपादित करें: यह वही है मैं अब तक है:

public class CustomComparator implements Comparator<Marker> { 
    @Override 
    public int compare(Mark o1, Mark o2) { 
     return o1.getDistance().compareTo(o2.getDistance()); 
    } 
} 

त्रुटि कहता है: आदिम प्रकार पर दो पर compareTo (डबल) आह्वान नहीं कर सकते।

क्या ऐसा इसलिए है क्योंकि एक तुलनित्र किसी निश्चित प्रकार के अलावा कुछ भी वापस नहीं कर सकता है?

+2

के लिए "मैं तुलनित्र के कुछ प्रकार के साथ Collections.sort() का उपयोग करना चाहिए? "हां, एक अच्छा विचार – Kennet

+0

जैसा लगता है कि मुझे पता नहीं है कि यह महत्वपूर्ण है लेकिन सूची में ऑब्जेक्ट्स की संख्या 80 जितनी अधिक होगी। यही कारण है कि अगर मैं जाने का तरीका हूं तो मैं तुलनित्र का उपयोग करने के बारे में उलझन में हूं क्योंकि यह केवल एक ही समय में दो मानों की तुलना करता है। –

+0

इस प्रकार सॉर्टिंग काम करता है। सबसे पहले एक आइटम को सूची में जोड़ें। अगला जोड़ते समय, इसे सूची में पहले या बाद में जाना चाहिए। तीसरे आइटम को जोड़ते समय सूची में पहले आइटम की तुलना करें, यदि उसके बाद अगले आइटम की तुलना करें। और इसी तरह। – Kennet

उत्तर

78

यदि आप एक डिफ़ॉल्ट प्रकार की तलाश में हैं तो आपको तुलनात्मक के बजाय तुलनात्मक उपयोग करना चाहिए।

यहाँ देखें, इस में कुछ मदद की हो सकती है - When should a class be Comparable and/or Comparator?

प्रयास करें इस -

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class TestSort { 

    public static void main(String args[]){ 

     ToSort toSort1 = new ToSort(new Float(3), "3"); 
     ToSort toSort2 = new ToSort(new Float(6), "6"); 
     ToSort toSort3 = new ToSort(new Float(9), "9"); 
     ToSort toSort4 = new ToSort(new Float(1), "1"); 
     ToSort toSort5 = new ToSort(new Float(5), "5"); 
     ToSort toSort6 = new ToSort(new Float(0), "0"); 
     ToSort toSort7 = new ToSort(new Float(3), "3"); 
     ToSort toSort8 = new ToSort(new Float(-3), "-3"); 

     List<ToSort> sortList = new ArrayList<ToSort>(); 
     sortList.add(toSort1); 
     sortList.add(toSort2); 
     sortList.add(toSort3); 
     sortList.add(toSort4); 
     sortList.add(toSort5); 
     sortList.add(toSort6); 
     sortList.add(toSort7); 
     sortList.add(toSort8); 

     Collections.sort(sortList); 

     for(ToSort toSort : sortList){ 
      System.out.println(toSort.toString()); 
     } 
    } 

} 

public class ToSort implements Comparable<ToSort> { 

    private Float val; 
    private String id; 

    public ToSort(Float val, String id){ 
     this.val = val; 
     this.id = id; 
    } 

    @Override 
    public int compareTo(ToSort f) { 

     if (val.floatValue() > f.val.floatValue()) { 
      return 1; 
     } 
     else if (val.floatValue() < f.val.floatValue()) { 
      return -1; 
     } 
     else { 
      return 0; 
     } 

    } 

    @Override 
    public String toString(){ 
     return this.id; 
    } 
} 
+0

हे लिंक के लिए धन्यवाद :) मैं एक भाषा से अगले भाषा में आगे और आगे जाता हूं इसलिए मैं हमेशा कुछ खो रहा हूं: पी आप जानते हैं कि यह कैसे चला जाता है ...... जैक सभी ट्रेडों लेकिन किसी भी मास्टर के मास्टर –

2

"एंड्रॉइड-जावा" यहां "सामान्य जावा" से अलग नहीं है, इसलिए हाँ Collections.sort() एक अच्छा दृष्टिकोण होगा।

+0

लेकिन मैं इसे ऑब्जेक्ट के भीतर किसी मान द्वारा कैसे क्रमबद्ध कर सकता हूं। मैं क्या अटक गया हूं। –

1

या तो Comparator बनाएं जो आपकी ऑब्जेक्ट्स की तुलना कर सकता है, या यदि वे एक ही कक्षा के सभी उदाहरण हैं, तो आप उस श्रेणी को Comparable लागू कर सकते हैं। फिर आप वास्तविक सॉर्टिंग करने के लिए Collections.sort() का उपयोग कर सकते हैं।

+0

मैं आगे बढ़ गया और मेरी कक्षा में तुलनात्मक लागू किया लेकिन इस पर कॉल करने के लिए एक विधि बनाई सूची जब मुझे इसे सॉर्ट करने की आवश्यकता होती है लेकिन मैं ऑब्जेक्ट में किसी मान के साथ इसे कैसे क्रमबद्ध करता हूं? –

+0

'तुलना करने के लिए) (-'ethod वह जगह है जहां आप तुलना करते हैं। थोड़ा गुगल ने उपयोग करने के तरीके पर कई विस्तृत उदाहरण दिए यह, उनमें से एक है: http://www.javadeveloper.co.in/java-example/java-comparable-example.html – Jave

+0

मैंने उदाहरण के समान एक विधि स्थापित की है, हालांकि मुझे एक त्रुटि मिल रही है जिसका उपयोग मैं नहीं कर सकता एक कास्ट डबल पर तुलना करें। ऐसा लगता है कि जो भी कारण है, मैं इसे क्या पसंद नहीं करता। तुलनात्मक रूप से डबल पर डबल (डबल) तुलना नहीं कर सकते हैं। मैं अपने कोड को ऊपर दिखाने के लिए ऊपर बताता हूं कि मेरा क्या मतलब है –

36

मुझे लगता है कि यह आपकी मदद करेंगे बेहतर

Person p = new Person("Bruce", "Willis"); 
Person p1 = new Person("Tom", "Hanks"); 
Person p2 = new Person("Nicolas", "Cage"); 
Person p3 = new Person("John", "Travolta"); 

ArrayList<Person> list = new ArrayList<Person>(); 
list.add(p); 
list.add(p1); 
list.add(p2); 
list.add(p3); 

Collections.sort(list, new Comparator() { 
    @Override 
    public int compare(Object o1, Object o2) { 
     Person p1 = (Person) o1; 
     Person p2 = (Person) o2; 
     return p1.getFirstName().compareToIgnoreCase(p2.getFirstName()); 
    } 
}); 
224

किसी भी ArrayList सॉर्ट करने के लिए इस कोड का पालन करें

Collections.sort(myList, new Comparator<EmployeeClass>(){ 
    public int compare(EmployeeClass obj1, EmployeeClass obj2) { 
     // ## Ascending order 
     return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values 
     // return Integer.valueOf(obj1.empId).compareTo(obj2.empId); // To compare integer values 

     // ## Descending order 
     // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values 
     // return Integer.valueOf(obj2.empId).compareTo(obj1.empId); // To compare integer values 
     } 
    }); 
+4

यह शीर्ष जवाब होना चाहिए! –

+1

सरल और महान उत्तर, कुडोस ब्रो :) – Sadashiv

+0

यह उदाहरण स्ट्रिंग की तुलना करने के लिए काम करता है। पूर्णांक की तुलना करके क्रमबद्ध करने के लिए प्रणव के उत्तर को नीचे देखें। – Ambran

2
public class DateComparator implements Comparator<Marker> { 
    @Override 
    public int compare(Mark lhs, Mark rhs) { 
     Double distance = Double.valueOf(lhs.getDistance()); 
     Double distance1 = Double.valueOf(rhs.getDistance()); 
     if (distance.compareTo(distance1) < 0) { 
      return -1; 
     } else if (distance.compareTo(distance1) > 0) { 
      return 1; 
     } else { 
      return 0; 
     } 
    } 
} 

ArrayList(Marker) arraylist; 

उपयोग कैसे करें:

Collections.sort(arraylist, new DateComparator()); 
0

मैं सूचीदृश्य जो सभी ग्राहकों मैं ग्राहकों को इस कस्टम तुलनित्र वर्ग का उपयोग करके नाम छँटाई कर रहा हूँ के बारे में जानकारी से पता चलता है। वे अंग्रेज़ी अक्षरों जो मैं इस setStrength (Collator.SECONDARY)

public class CustomNameComparator implements Comparator<ClientInfo> { 
     @Override 

    public int compare(ClientInfo o1, ClientInfo o2) { 

     Locale locale=Locale.getDefault(); 
     Collator collator = Collator.getInstance(locale); 
     collator.setStrength(Collator.SECONDARY); 
     return collator.compare(o1.title, o2.title); 

    } 
} 


PRIMARY strength: Typically, this is used to denote differences between base characters (for example, "a" < "b"). It is the strongest difference. For example, dictionaries are divided into different sections by base character. 
SECONDARY strength: Accents in the characters are considered secondary differences (for example, "as" < "às" < "at"). Other differences between letters can also be considered secondary differences, depending on the language. A secondary difference is ignored when there is a primary difference anywhere in the strings. 
TERTIARY strength: Upper and lower case differences in characters are distinguished at tertiary strength (for example, "ao" < "Ao" < "aò"). In addition, a variant of a letter differs from the base form on the tertiary strength (such as "A" and "Ⓐ"). Another example is the difference between large and small Kana. A tertiary difference is ignored when there is a primary or secondary difference anywhere in the strings. 
IDENTICAL strength: When all other strengths are equal, the IDENTICAL strength is used as a tiebreaker. The Unicode code point values of the NFD form of each string are compared, just in case there is no difference. For example, Hebrew cantellation marks are only distinguished at this strength. This strength should be used sparingly, as only code point value differences between two strings are an extremely rare occurrence. Using this strength substantially decreases the performance for both comparison and collation key generation APIs. This strength also increases the size of the collation key. 

**Here is a another way to make a rule base sorting if u need it just sharing** 

/*  String rules="< å,Å< ä,Ä< a,A< b,B< c,C< d,D< é< e,E< f,F< g,G< h,H< ï< i,I"+"< j,J< k,K< l,L< m,M< n,N< ö,Ö< o,O< p,P< q,Q< r,R"+"< s,S< t,T< ü< u,U< v,V< w,W< x,X< y,Y< z,Z"; 
     RuleBasedCollator rbc = null; 
     try { 
      rbc = new RuleBasedCollator(rules); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     String myTitles[]={o1.title,o2.title}; 
     Collections.sort(Arrays.asList(myTitles), rbc);*/ 
2

साथ प्रबंध कर रहा हूँ आप इस का उपयोग करते हुए दो स्ट्रिंग तुलना कर सकते हैं से अलग कुछ अतिरिक्त lerret कर रहे हैं।

Collections.sort(contactsList, new Comparator<ContactsData>() { 

        @Override 
        public int compare(ContactsData lhs, ContactsData rhs) { 

         char l = Character.toUpperCase(lhs.name.charAt(0)); 

         if (l < 'A' || l > 'Z') 

          l += 'Z'; 

         char r = Character.toUpperCase(rhs.name.charAt(0)); 

         if (r < 'A' || r > 'Z') 

          r += 'Z'; 

         String s1 = l + lhs.name.substring(1); 

         String s2 = r + rhs.name.substring(1); 

         return s1.compareTo(s2); 

        } 

       }); 

और अब एक संपर्क डेटा कक्षा बनाएं।

public class ContactsData { 

public String name; 
public String id; 
public String email; 
public String avatar; 
public String connection_type; 
public String thumb; 
public String small; 
public String first_name; 
public String last_name; 
public String no_of_user; 
public int grpIndex; 

public ContactsData(String name, String id, String email, String avatar, String connection_type) 
{ 
    this.name = name; 
    this.id = id; 
    this.email = email; 
    this.avatar = avatar; 
    this.connection_type = connection_type; 

} 
} 

यहाँ contactsList है:

public static ArrayList<ContactsData> contactsList = new ArrayList<ContactsData>(); 
+1

आप सबसे अच्छे आदमी हैं, बहुत बहुत धन्यवाद –

0

मॉडल कक्षा:

public class ToDoModel implements Comparable<ToDoModel> { 
    private String id; 
    private Date taskDate; 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public Date getTaskDate() { 
     return taskDate; 
    } 

    public void setTaskDate(Date taskDate) { 
     this.taskDate = taskDate; 
    } 

    @Override 
    public int compareTo(ToDoModel another) { 
     return getTaskDate().compareTo(another.getTaskDate()); 
    } 
} 

ArrayList

0 में अब सेट डेटा

अब क्रमबद्ध ArrayList

Collections.sort(toDoList); 

सारांश: यह आपके डेटा datewise

12

अब (मुक्केबाजी करने की कोई जरूरत यानी नए ऑपरेटर का उपयोग valueOf का उपयोग कर OBJECT बनाना करने की कोई जरूरत को सॉर्ट होगा insted तुलना के साथ संग्रह। .. ..

1) आरोही क्रम के लिए

Collections.sort(temp, new Comparator<XYZBean>() 
{ 
    @Override 
    public int compare(XYZBean lhs, XYZBean rhs) { 

     return Integer.valueOf(lhs.getDistance()).compareTo(rhs.getDistance()); 
     } 
}); 

1) Deascending आदेश

Collections.sort(temp, new Comparator<XYZBean>() 
{ 
    @Override 
    public int compare(XYZBean lhs, XYZBean rhs) { 

     return Integer.valueOf(rhs.getDistance()).compareTo(lhs.getDistance()); 
     } 
}); 
संबंधित मुद्दे