Home:ALL Converter>Object Array Sorting

Object Array Sorting

Ask Time:2013-02-05T11:22:52         Author:john doe

Json Formatter

I understand the concept of bubble sorting an array of Int but is it possible to apply bubble sorting to an array of objects? Specifically I want to sort by an int variable contained within the objects.

Int array only has 1 Int or data member to one element of the array, but Objects has multiple data members and that's the part that really confuses me.

also I've looked into Comparetors such as the CompareTo method of java. From what I learned from doing research on it, Comparetors basically returns the difference in value between two objects. How can I apply this to sorting an array?

Author:john doe,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14699672/object-array-sorting
Jack :

A Comparator<T> does not return the difference between two objects. It rather returns the ordering of two respective instances of T.\n\nAs documentation states:\n\n\n Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.\n\n\nThe good thing of OOP is the fact that you are able to encapsulate data and behavior and this situation is a clear example. Let's look at this boilerplate code:\n\nclass HugeClass implements Comparable<HugeClass> {\n // lots of variables\n\n public int compareTo(HugeClass o) {\n // lots of code to decide if this instance is lesser, equal or greater than o\n // then we return -1, 0 or 1 accordingly\n }\n\n\nNow you can implement your bubble sort easily. Instead that directly doing if (x < y), as you would do with ints you will do if (x.compareTo(y) < 0)but that's the same story and you don't care about how the ordering is actually computed, you just rely on the compareTo method.\n\nActually you can write a bubble sort method that is able to perform sorting on every comparable class possible. Eg:\n\npublic sort(Comparable<?>[] data) {\n .. code\n}\n",
2013-02-05T03:30:06
Floris :

Here is a way to think about your confusion:\n\nImagine I have a set of boxes with numbers on them. In the boxes are many objects: big, small, red, green, ... If I ask you to sort the boxes (\"objects\") by the number on them, you would have no problem. If I asked you instead to look inside the box, and sort them by the size of the green balls, you could probably do it. In either case you are sorting objects...",
2013-02-05T03:29:57
liuyuanhui0301 :

Maybe this can help you for your confusion:\n\npublic class Test {\n\n public static A a1 = new A(\"001\", \"a\", \"wa~\", 7);\n public static A a2 = new A(\"001\", \"c\", \"wa~\", 9);\n public static A a3 = new A(\"001\", \"b\", \"wa~\", 3);\n public static A a4 = new A(\"000\", \"d\", \"wa~\", 6);\n\n public static void main(String[] args) {\n A[] voA = new A[] { a1, a2, a3, a4 };\n\n java.util.Arrays.sort(voA, new Mycomparator());\n\n A[] newA = new A[voA.length];\n\n for (int i = 0; i < voA.length; i++)\n System.out.println(\"[\" + i + \"]:\" + voA[i].A1 + \" \" + voA[i].A2 + \" \" + voA[i].A3 + \" \" + voA[i].A4);\n }\n}\n\nclass A {\n String A1;\n String A2;\n String A3;\n int A4;\n\n public A(String oA1, String oA2, String oA3, int oA4) {\n this.A1 = oA1;\n this.A2 = oA2;\n this.A3 = oA3;\n this.A4 = oA4;\n }\n\n}\n\nclass Mycomparator implements java.util.Comparator {\n public int compare(Object o1, Object o2) {\n A a1 = (A) o1;\n A a2 = (A) o2;\n if(a1.A1.compareTo(a2.A1)!=0) {\n return a1.A1.compareTo(a2.A1);\n } else if(a1.A2.compareTo(a2.A2)!=0) {\n return a1.A2.compareTo(a2.A2);\n } else if(a1.A3.compareTo(a2.A3)!=0) {\n return a1.A3.compareTo(a2.A3);\n } else {\n return a1.A4 >a2.A4?1:a1.A4==a2.A4?0:-1;\n }\n }\n}\n",
2013-02-05T03:32:32
yy