Sorting My objects
I have a class with some fields.
I want to sort the objects based on a particular field.
How to use comparable(Oject o)
I've posted an example here,
http://forum.java.sun.com/thread.jsp?forum=31&thread=448348
Similar Messages
-
Need to sort an object array using an element in the object.
hi all,
i need to sort an object array using an element in the object.can someone throw some light on this.
Edited by: rageeth on Jun 14, 2008 2:32 AM[http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]
-
How to sort a object vector by its integer item ?
hi everybody,
[there were few topics on this in the forum, but nothing could solve my problem yet. so, please instruct me]
I have to sort a vector which contains objects, where each object represents, different data types of values,
ex: {obj1, obj2, obj3, ....}
obj1---->{String name, int ID, String[] departments}
i have to sort this vector at three times, once by name, then by ID and then by departments.
Leaving name and department , first i want to sort the ID of each object and then re-arrange the order of objects in the array according to new order.
what i did was, copied the vector all objects' ID values to an integer array then i sorted it using selection sort. but now i want to re-arrange the vector, but i still can't. please guide.
here is the sort i did, and the
int[] ID = new int[mintomaxID.size()];
for(int i=0;i<mintomaxID.size();i++)
ObjectStore_initialData obj_id = (ObjectStore_initialData)mintomaxID.elementAt(i);
ID[i] = obj_id.getID();
System.out.println("Before sorting array");
for(int i=0;i<ID.length;i++)
System.out.println(ID);
System.out.println();
int i, j, m, mi;
for (i = 0; i < ID.length - 1; i++) {
/* find the minimum */
mi = i;
for (j = i+1; j < ID.length; j++) {
if (ID[j] < ID[mi]) {
mi = j;
m = ID[mi];
/* move elements to the right */
for (j = mi; j > i; j--) {
ID[j] = ID[j-1];
ID[i] = m;
System.out.println("After sorting array");
for(int y=0;y<ID.length;y++)
System.out.println(ID[y]);
//*****here is where i need to re-arrange the entire vector by ID.
I do understand this is some sort of database sort type of a question. but there's no database. it's simply i just want to sort the vector.
Thank you.hi camickr,
thank you for the detailed reply. but i still don't understand somethings. i tried to read the API and look for the collections.
i have ObjectStore_initialData class (similar to person class), so do i still have to do a comparable class out of this class ? please simplify that.
public class ObjectStore_initialData
String NAME;
int ID;
String[] DPART;
public ObjectStore_initialData(String name, int id, String[] departments)
NAME=name;
ID=id;
DPART = departments;
public String getName()//----
return NAME;
public String getID()//----
return ID;
public String getDpart()//----
return DPART;
/*next class is the interface to collect the values from the user and put all of them at once in a vector*/
//this class is to sort the vector by ID
public class sorter
public sorter()
public static void sortbyID(Vector mintomaxID)
int[] ID = new int[mintomaxID.size()];
for(int i=0;i<mintomaxID.size();i++)
ObjectStore_initialData obj_id = (ObjectStore_initialData)mintomaxID.elementAt(i);
ID[i] = obj_id.getID();
System.out.println("Before sorting array");
for(int i=0;i<ID.length;i++)
System.out.println(ID);
System.out.println();
int i, j, m, mi;
for (i = 0; i >< ID.length - 1; i++) {
/* find the minimum */
mi = i;
for (j = i+1; j < ID.length; j++) {
if (ID[j] < ID[mi]) {
mi = j;
m = ID[mi];
/* move elements to the right */
for (j = mi; j > i; j--) {
ID[j] = ID[j-1];
ID[i] = m;
System.out.println("After sorting array");
for(int y=0;y<ID.length;y++)
System.out.println(ID[y]);
//*****here is where i need to re-arrange the entire vector by ID.
/*new comparable class */
public class ObjectStore_initialData implements Comparable
String NAME;
int ID;
String[] DPART;
public ObjectStore_initialData(String name, int id, String[] departments)
NAME=name;
ID=id;
DPART = departments;
public String getName()//----
return NAME;
public String getID()//----
return ID;
public String getDpart()//----
return DPART;
static class IDComparator implements Comparator
public int compare(Object o1, Object o2)
ObjectStore_initialData p1 = (ObjectStore_initialData )o1;
ObjectStore_initialData p2 = (ObjectStore_initialData )o2;
return p1.getID() - p2.getID();
/*how can i put the vector here to sort? */
public sorter()
public static void sortbyID(Vector mintomaxID)
int[] ID = new int[mintomaxID.size()];
for(int i=0;i<mintomaxID.size();i++)
ObjectStore_initialData obj_id = (ObjectStore_initialData)mintomaxID.elementAt(i);
ID[i] = obj_id.getID();
System.out.println("Before sorting array");
for(int i=0;i<ID.length;i++)
System.out.println(ID[i]);
System.out.println();
int i, j, m, mi;
for (i = 0; i >< ID.length - 1; i++) {
/* find the minimum */
mi = i;
for (j = i+1; j < ID.length; j++) {
if (ID[j] < ID[mi]) {
mi = j;
m = ID[mi];
/* move elements to the right */
for (j = mi; j > i; j--) {
ID[j] = ID[j-1];
ID[i] = m;
System.out.println("After sorting array");
for(int y=0;y<ID.length;y++)
System.out.println(ID[y]);
/* using collections to sort*/
Collections.sort(mintomaxID, new IDComparator());
and to check the new order i wanted to print the vector to command line.
still it doesn't do anything.
the url you mentioned is good as i see. but how can i implement that in my class ? please instruct and simplify. i know i just repeated the code, i didn't understand to do a comparable class in collections for this class. Please explain where i'm head to and where's my misleading point here.
Thank you.
Message was edited by:
ArchiEnger.711 -
Sort and Object array of Arrays
Hi,
I have an object array that contains a String[] and an int[].
Object[String[], int[]]
The values in the Strings array must be in the same position in the array as those in the int array i.e
String [0] is the string representation of int[0].
Its for a bar chart.
That is all working beautifully.
Now however I want to sort the int array in descending order by the value of the ints in the array and thus the order or the String array has to mirror this.
I am using a comparator to do this but it just wont work. I'm sure this is the way but how should it look?
Has anybody got any ideas?
Thanks in advance,
L.use the bubblesort:
for (int i = a.length; --i >= 0; ) {
for (int j = 0; j < i; j++) {
if (a[j][1] > a[j+1][1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
} -
Force z-sorting of objects?
I am trying to create a 3d interface that displays on
top/front of the 3d scene. I have different objects I want to
display in the correct order but because of perspectives of
different cameras, and the moving/animating of some objects, they
don't always display in the order I want them to.
So, is there a way to force some 3d objects to draw on top
of/in front of other objects? (can't use overlay, must be 3d
objects)
thanks!!!http://java.sun.com/j2se/1.4.1/docs/api/java/util/Arrays.html#sort(java.lang.Object[])
public static void sort(Object[] a) "[t]hrows:
ClassCastException - If the array contains elements that are not mutually comparable (for example, string and integers)." -
As far as I could see, a similar problem has been reported for earlier versions of SQL Developer, but still seems to apply for some parts in 4.0.0.13.80 under Windows7 64bit with JDK 1.7.0_45 64bit ...
Currently not all objects in a generated DDL using "Quick DDL" or "Tools - Database Export" are sorted evenly ... tables and foreign key constraints seem to be sorted, but common constraints are not.
This is rather annoying when trying to compare the generated scripts of two databases. Or trying to detect changes over lifetime (SVN etc.).
Or is there an option for this I could not find/see so far?
example output from database export of SQLDev 4.0.0.13.80 on the same machine, within some minutes, no configuration change, for a local and a remote database ...
from local:
Questions:
why are there blank lines added for the local database
why aren't the grants in the same order
local database
remote database
GRANT DELETE, INSERT, SELECT, UPDATE ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "ACCESS_ROLE_FOR_SCHEMA_3";
GRANT REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_3";
GRANT REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_4";
GRANT SELECT, REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_5";
GRANT SELECT, REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_6";
GRANT SELECT ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_7";
GRANT REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_3";
GRANT REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_4";
GRANT SELECT, REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_5";
GRANT DELETE, INSERT, SELECT, UPDATE ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "ACCESS_ROLE_FOR_SCHEMA_3";
GRANT SELECT, REFERENCES ON "FOREIGNSCHEMA"."TABLE_FOR_GRANT" TO "SCHEMA_6";
remark: missing SCHEMA_7 is okay, because it is not yet on the remote database
Similar applies to non foreign key constraints, where the unsorted output is even worse ...
Message was edited by: stuecklDid you ever figure out why SQL Developer isn't exporting the NOT NULL attribute of columns?
I'm running into that on a 9i database, but not on 11g.
Skip -
Sort an object by different fields
Hello!
Sorry for the (maybe) bad englisch but I'm a German.
If I have an object fith different fields for example field1, field2, field3.
To write a method which sorts an array of this objects by a given field?
I mean something like this
sort(String field, object[] o) {
//sort by field
instead of this
sortbyfield1(object o){
//sort by "field1"
sortbyfield2(object[] o){
//sort by "field2"
hope you understood
thanks
StefanWhen faced with this problem I usually use a Comparator helper class. Depending on whether the Comparator can be re-used I'll create a separate class or if not an inner or anonymous inner class. For example:
import java.util.Comparator;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
public class Main {
// Compares Fields.f1 ascending
public Comparator c1 = new Comparator() {
public int compare(Object o1,
Object o2) {
return ((Main.Fields)o1).f1.compareTo(((Main.Fields)o2).f1);
public void sortem() {
// Create our array of objects
Object[] array = new Object[] {
new Fields(new Integer(1),3,"3"),
new Fields(new Integer(2),2,"1"),
new Fields(new Integer(3),1,"2")
// Sort on Field.f1 using reusable comparator
Arrays.sort(array,c1);
System.out.println(Arrays.asList(array));
// Sort on Field.f2 using inner class
Arrays.sort(array,new C2());
System.out.println(Arrays.asList(array));
// Sort on f3 using an anonymous inner class
Arrays.sort(array, new Comparator(){
public int compare(Object o1,
Object o2) {
return((Main.Fields)o1).f3.compareTo(((Main.Fields)o2).f3);
System.out.println(Arrays.asList(array));
public static void main(String[] args) {
Main m = new Main();
m.sortem();
// Class with many fields of different types
class Fields {
public Integer f1;
public int f2;
public String f3;
public Fields(Integer f1, int f2, String f3) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
public String toString() {
return ""+f1+" "+f2+" "+f3;
// Inner class comparator on Field.f2
class C2 implements Comparator {
public int compare(Object o1,
Object o2) {
int result = 0;
int i1 = ((Main.Fields)o1).f2;
int i2 = ((Main.Fields)o2).f2;
if (i1==i1) result = 0;
if(i1<i2) result = -1;
else if (i1>i2) result = 1;
return result; -
Writing a function to that sorts any object
I am trying to write a function that sorts any type of array of objects. For example, I want to call
NewArray = exampleclass.sort(ObjectOfAnyType).
The sort function has to return a new array. I have run into two errors, however.
Error #1
as3.java [21:1] cannot resolve symbol
symbol : variable Object
location: class as3
if (Object instanceof Comparable)
^
Error #2
as3.java [25:1] incompatible types
found : java.lang.Object
required: java.lang.Object[]
Object [] rtn = obj.clone();
*** Below is my source code ***
import java.*;
public class as3 {
/** Creates new as3 */
public static Object sort(Object [] obj)
if (Object instanceof Comparable)
if (Object instanceof Cloneable)
Object [] rtn = obj.clone();
for (int i = 0; i<obj.length-1; i++)
for (int j=i+1; j<obj.length; j++)
if (rtn.compareTo(rtn[j]) == 1)
Object [] tmp = rtn[i];
rtn[i] = obj[j];
rtn[j] = tmp;
else
throw new ObjectIsNotComparableException("Object not comparable");
Any help appreciated. Thanks!Changing from "Object" to the name "obj" fixed the
statement:
if (obj instanceof Cloneable) but not if (obj
instanceof Comparable).
I don't know what's causing this one yet... worry about that in a sec
I am also getting one new error now:
as3.java [28:1] cannot resolve symbol
symbol : method compareTo (java.lang.Object)
location: class java.lang.Object
if (rtn[ i].compareTo(rtn[j])
This one is because rtn[ i] (and rtn[j]) is an Object, which has no method compareTo. Try casting it to a Comparable...
I think I just worked out why it let you go with the cloneable, but not with the comparable. obj is an array, and sure an array is cloneable, but how could you compare an array? Sure you can compare the elements in the array... So either check if obj[0] instanceof Comparable, or drop the test all together.
Note: checking obj[0] will give you an exception if someone asks you to sort an array of 0 length (no elements)
Hope this helps,
Radish21 -
Sort view object rows with transient attributes
Hi ,
Is it possible to sort the results of a view object based on the populated transient attributes ?
Best regards
KK,
Have you had a look at section 27.5 of the ADF Developer's Guide for Forms/4GL Developers ("Performing In-Memory Sorting and Filtering of Row Sets") - I've not tried it with transient attributes, but it does describe how to do in-memory sorting, as opposed to the normal way of sorting which is to send an ORDER BY to the database.
Hope this helps,
John -
I have an object defined as
CREATE OR REPLACE
type VALID_NAP AS object(
ID NUMBER(10),
NT_ID NUMBER(10),
BEGIN_DATE DATE,
END_DATE DATE,
TABLE_SOURCE VARCHAR2(10));
I have a nested table of objects defined as
CREATE OR REPLACE
TYPE LIST_VALID_NAP
AS TABLE OF VALID_NAP;
After inserting records into this nested table, as below
ID NT_ID BEGIN_DATE END_DATE TABLE_SOURCE
6,000.00 23.00 6/1/08 6/30/08 NAT_AH
6,000.00 58.00 5/1/08 5/31/08 NAT_AH
6,000.00 58.00 4/1/08 NAT_AH
6,000.00 97.00 1/1/08 1/31/08 NAT_AH
6,000.00 12.00 2/1/08 3/31/08 NAT
6,000.00 12.00 12/1/07 12/31/07 NAT
6,000.00 91.00 11/1/07 11/30/07 NAT_AH
I need to sort the above records with dates in descending order. Have can I sort a collection of objects?Have can I sort a collection of
objects?
SQL> create or replace type t1 as object(id number,amt number);
2 /
Type created.
SQL> create type t2 as table of t1;
2 /
Type created.
SQL> create table t3(c1 number,c2 t2) nested table c2 store as c2_store;
Table created.
SQL> insert into t3 values(1,t2(t1(1,100),t1(2,200),t1(1.5,150)));
1 row created.
SQL> select *
2 from table(select c2 from t3);
ID AMT
1 100
2 200
1.5 150
SQL> select *
2 from table(select c2 from t3)
3 order by id;
ID AMT
1 100
1.5 150
2 200 -
Sort Nested Object - 2 properties
Hello,
I am trying to sort based on a few properties of an object. The only issue i am having is the properties are within a nested object.
For example.
I would like to stort the following object by StreetNumber and StreetName, but these properties are inside 'locations_attributes' array.
"commute": {
"minutes": 0,
"startTime": "Wed May 06 22:14:12 EDT 2009",
"locations_attributes": [
"StreetNumber": "12",
"StreetName": "Main"
"StreetType": "St"
"StreetNumber ": "17",
"StreetName": "Morning Side ",
"StreetType": "Dr"
"StreetNumber ": "26",
"StreetName": "Blake",
"StreetType": "St"
Can this be done ?
DrewI get a JSON result from a REST service and I have a custom component that I populate.
I am not usin a datagrid, but a extended version of the List component.
I am just trying to sort the returned result sorted by street number and street name.
I was using a compareFunction in a sort() method, but only found samples for sorting off of one field.. not 2.
Here is is a sample of what I was using for 1 field.
private function sortByAttribute(a:Object, b:Object):Object
var x:String = a.attributes[this._activePanel.SortField].toLowerCase();
var y:String = b.attributes[this._activePanel.SortField].toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); -
Userdefined Sorting the objects using comparator
Hi All,
I want to display the objects in a userdefined order. I have placed the code snippet below. Kindly help me on this to resolve this issue.
public class ApplicabilityObject1 implements Comparable{
private String str;
public ApplicabilityObject1(String str) {
this.str = str;
public boolean equals(Object obj) {
return getStr().equals(((ApplicabilityObject1)obj).getStr());
public String toString() {
return str;
public String getStr() {
return str;
public void setStr(String str) {
this.str = str;
public int compareTo(Object arg0) {
final int equal = 0;
final int before = -1;
final int after= 1;
int returnvalues = 0;
System.out.println(this);
if ("Mexico"==((ApplicabilityObject1)arg0).getStr())
returnvalues = -1;
else if (((ApplicabilityObject1)arg0).getStr()== "Canada")
returnvalues = 0;
else if (((ApplicabilityObject1)arg0).getStr()== "USA")
returnvalues = 1;
return returnvalues;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.FixedOrderComparator;*/
public class SortOrder {
* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
APPComparator app1 = new APPComparator();
ApplicabilityObject1 obj1 = new ApplicabilityObject1("Mexico");
ApplicabilityObject1 obj2 = new ApplicabilityObject1("Canada");
ApplicabilityObject1 obj3 = new ApplicabilityObject1("USA");
ApplicabilityObject1 obj4 = new ApplicabilityObject1("Mexico");
ApplicabilityObject1 obj5 = new ApplicabilityObject1("USA");
ApplicabilityObject1 obj6 = new ApplicabilityObject1("USA");
ApplicabilityObject1 obj7 = new ApplicabilityObject1("Mexico");
ApplicabilityObject1 obj8 = new ApplicabilityObject1("Mexico");
ApplicabilityObject1 obj9 = new ApplicabilityObject1("Canada");
List list = new ArrayList();
list.add(obj1);
list.add(obj2);
list.add(obj3);
list.add(obj4);
list.add(obj5);
list.add(obj6);
list.add(obj7);
list.add(obj8);
list.add(obj9);
Collections.sort(list, app1);
System.err.println(list);
System.out.println(Integer.MAX_VALUE);
class APPComparator implements Comparator
ApplicabilityObject1 appO = new ApplicabilityObject1("USA");
@Override
public int compare(Object arg0, Object arg1) {
// TODO Auto-generated method stub
return ((ApplicabilityObject1)arg0).compareTo(arg1);
}I'm expecting the result in the Order of USA, CANADA, MEXICO.
But now the above code giving the result of [USA, USA, USA, Mexico, Canada, Mexico, Mexico, Mexico, Canada]
Kindly help me to resolve this issue.
Thanks in advance,
MaheswaranAn alternative way to reduce the size of your code.
//Un-Compiled
ApplicabilityObject1[] appObs = {
new ApplicabilityObject1("Mexico"),
new ApplicabilityObject1("Canada"),
new ApplicabilityObject1("USA"),
new ApplicabilityObject1("Mexico"),
new ApplicabilityObject1("USA"),
new ApplicabilityObject1("USA"),
new ApplicabilityObject1("Mexico"),
new ApplicabilityObject1("Mexico"),
new ApplicabilityObject1("Canada")};
List list = new ArrayList(Arrays.asList(appObs));Mel -
Possible to sort No object table's records?
Hi,
Is it possible to sort the records of No object table that is accessible by clicking Tools-User tables?
by default, it sorts on the basis of the 'code' column but, since it is of character type, the sorting is not numerically correct. it sorts like 1,10,11,12,....19, 2,20,...and so on.
thanks and regards,
Binita JoshiHi Binita,
i know this problem and its weird - you can't sort it.
thats why i generate codes always with 0 before - than it is sorted.
like
00001
00002
00011
that's how iam doing.
lg David -
Passing record properties to a sort script object
Hello,
I'm still at the beginning of the Applescript learning curve, and as a fairly seasoned programmer on mainframes and VB, I'm anxious to start off in the right direction.
I have developed / purloined a fast sorting algorithm to sort records. It is a stand alone script that is loaded by the script that requires its services. The problem is that in its current guise, it sorts on a specific property within the record. What I would like to do is to make this script truly 'utility' by adding the capability to sort on any property of a record. The 64,000 dollar question is, how do I pass the name of the records property on which to sort into the script?
If possible, could a sample of the code that will be required be included.
ThanksHello
AppleScript's record label is not easy to deal with programmatically because it must be statically defined at compile time. E.g., you cannot do this :
set k to "key1"
{key1:10, key2:20}'s k -- Error -1728 : Can't get k of {key1:10, key2:20}.
Because of this, any attempt to pass record label as handler's parameter would fail in plain AppleScript.
The simplest way to implement universal sort handler would be to define comparator as external handler and pass the handler's reference to the sort handler as its parameter.
Having the comparator encapsulate the comparing logic of the data, the sorting logic of sort handler can be independent of the internal structure of the data.
Something like the code below, which is using my sort handler in service.
Hope this may help,
H
--SCRIPT
return sortrecordseg()
--return sortscalarseg()
on sortrecordseg()
set rr to {}
repeat 20 times
set end of rr to {key1:random number 20, key2:random number 20}
end repeat
msort(my cmpreco_key1a, rr) -- ascending by key1
--msort(my cmpreco_key2d, rr) -- descending by key2
--msort(my cmpreco_key1_a_key2d, rr) -- ascending by key1, descending by key2
return rr
end sortrecordseg
on sortscalarseg()
set rr to {}
repeat 20 times
set end of rr to random number 20
end repeat
msort(my cmpscalara, rr) -- ascending
--msort(my cmpscalard, rr) -- descending
return rr
end sortscalarseg
on cmpscalara(x, y)
(* compartor for msort handler
sort scalars in ascending order *)
x > y
end cmpscalara
on cmpscalard(x, y)
(* compartor for msort handler
sort scalars in ascending order *)
x < y
end cmpscalard
on cmpreco_key1a(x, y)
(* comparator for msort handler
sort records in ascending order by key1 *)
x's key1 > y's key1
end cmpreco_key1a
on cmpreco_key2d(x, y)
(* compartor for msort handler
sort records in descending order by key2 *)
x's key2 < y's key2
end cmpreco_key2d
on cmpreco_key1_a_key2d(x, y)
(* compartor for msort handler
sort records in ascending order by key1, descending order by key2 *)
local xk, yk
set xk to x's key1
set yk to y's key1
if xk = yk then
return x's key2 < y's key2
else
return xk > yk
end if
end cmpreco_key1_a_key2d
on msort(cmp_, aa) -- v1.2f2
Basic recursive merge sort handler having list sorted in place
handler cmp_ : comparator
* cmp_(x, y) must return true iff list element x and y are out of order.
list aa : list to be sorted in place
script o
property parent : {} -- limit closure to minimum
property xx : aa -- to be sorted in place
property xxl : count my xx
property yy : {}
property cmp : cmp_
on merge(p, q, r)
property xx: source list
integer p, q, r : absolute indices to specify range to be merged such that
xx's items p thru r is the target range,
xx's items p thru (q-1) is the first sublist,
xx's items q thru r is the second sublist.
(p < q <= r)
local i, j, k, xp, xr, yi, yj, ix, jx
if r - p = 1 then
set xp to my xx's item p
set xr to my xx's item r
if my cmp(xp, xr) then
set my xx's item p to xr
set my xx's item r to xp
end if
return -- exit
else
if p < q - 1 then merge(p, (p + q) div 2, q - 1)
merge(q, (q + r + 1) div 2, r)
end if
At this point, sublits xx[p, q-1] and xx[q, r] have been already sorted (p < q <= r)
if my cmp(my xx's item (q - 1), my xx's item q) then
else -- xx[p, q-1] & xx[q, r] are already sorted
return
end if
set yy to my xx's items p thru r -- working copy for comparison
set ix to q - p
set jx to r - p + 1
set i to 1
set j to q - p + 1
set k to p
set yi to my yy's item i
set yj to my yy's item j
repeat
if my cmp(yi, yj) then
set my xx's item k to yj
set j to j + 1
set k to k + 1
if j > jx then
set my xx's item k to yi
set i to i + 1
set k to k + 1
repeat until k > r
set my xx's item k to my yy's item i
set i to i + 1
set k to k + 1
end repeat
return
end if
set yj to my yy's item j
else
set my xx's item k to yi
set i to i + 1
set k to k + 1
if i > ix then
set my xx's item k to yj
set j to j + 1
set k to k + 1
repeat until k > r
set my xx's item k to my yy's item j
set j to j + 1
set k to k + 1
end repeat
return
end if
set yi to my yy's item i
end if
end repeat
end merge
on cmp(x, y)
(* primary comparator *)
return x > y
end cmp
local d, i, j
if xxl ≤ 1 then return
if cmp_ = {} then set my cmp to cmp -- comparator fallback
my merge(1, (xxl + 1) div 2, xxl)
end script
tell o to run
end msort
--END OF SCRIPT -
Array sort with objects?
hits = searcher.search(query);
//java.util.Vector sortHits = new java.util.Vector ();
int[][] sortHits = new int[hits.length()][1];
// assign values to array
int teller = 0;
for (int i = 0; i < hits.length(); i++)
Document doc = hits.doc(i);
String absnumtxt = doc.get("absnumtxt");
if(sAllCookies.indexOf(absnumtxt) >= 0){
sortHits[0] = Integer.parseInt(absnumtxt);
teller++;
continue;
But the method Arrays.sort(sortHits); causes a ClassCastException error. Somebody an idea to solve this?
The structure is:
sortHits[0][0] = 2
sortHits[1][0] = 0
sortHits[2][0] = 0
sortHits[3][0] = 0
sortHits[4][0] = 0
sortHits[5][0] = 0A two-dimentional array is an array of array of something.
Sorting a two-dimentional array requires a Comparator, because you compare uni-dimentional arrays, and arrays are not naturally comparable.
(BTW, why declaring a two-dimentional array here? you set the second dimention length to 1... a simple array would make it, no?)
Maybe you are looking for
-
Can I install a bigger hard drive on my HP Laptop dv9230us
Can I install a bigger hard drive on my HP Laptop dv9230us
-
Why is that when I am composing a new message do I end up with multiple versions of my message I am composing up in my Google Trash mailbox? How can I stop this from happening?
-
How do I uninstall Quick Time Player 10 from a Mac
I cannot find an article that explains how to uninstall Quick Time Player, version 10, from my Mac Snow Leopard. I need to install Quick Time 7 in order to install Quick Time Pro 7. Just moving the program to the Trash did not help. Thanks.
-
new Computer C:drive with win 7 is 40gb, E:drive is 1tb old computer C:drive with win 7 is 500gb, D:drive is 80gb I want to transfer the contents of my old computer (400gb) to the E:drive on the new comp
-
How to backup macbook in recovery mode?
My laptop is crashed. I've no backup. I can open it on recovey mode but cannot access to any of my data. I can see disk utility but i donot understand how to backup files from there. Before i format my laptop i need to backup few importnant files. Is