java - Abstract Data Types, sorting objects by specific variable in a list -
alright here go.
i made adt in form of sortedarraylist, has add method looking this:
public boolean addtoarray(t i) { int insertplace = 0; for(int j=0;j<size;j++) { if(i.compareto(sortedarray[j])<0) { insertplace =j; j = size; } } if(size>0) { for(int w=size-1; w>=insertplace;w--) { sortedarray[size]=sortedarray[w]; } sortedarray[insertplace]=i; } else { sortedarray[0]=i; } size++; return true; }
now, works wonders sorted adt when input strings. however, instead of strings, want add objects list in shape of persons, object containing 4 variables(string country, string name, int age, int cpr). want person objects sorted in list age.
here person class compareto method sorting.
public class person implements comparable<person> { int cpr=200193; int age=21; string name="john doe"; string country="uzbekistan"; public person() { this.cpr=cpr; this.age=age; } public person(string name, string country,int cpr,int age) { this.cpr=cpr; this.age=age; this.name=name; this.country=country; } @override public string tostring() { return "person [country= " + country + ", name:" + name + ", cpr: "+cpr+ ", age: "+age+"]"; } public int compareto(person p) { int before=-1; int after=1; int middle=0; if(this.age!=p.age) { if(this.age>p.age) { return before; } if(this.age<p.age) { return after; } } return middle; }
now, problem objects sorted, since not appear in same order call them list. can't figure out how sorted , how make objects sorted age in list.
edit
p1.addtoarray(new person()); p1.addtoarray(new person("pete","germany",111111,86)); p1.addtoarray(new person("john","denmark",123456,75)); p1.addtoarray(new person("michael jackson", "america",112345,49));
output:
item: person [country= america, name:michael jackson, cpr: 112345, age: 49] item: person [country= uzbekistan, name:john doe, cpr: 200193, age: 21] item: person [country= germany, name:pete, cpr: 111111, age: 86] item: person [country= denmark, name:john, cpr: 123456, age: 75]
i don't think sorting function works:
my test:
static void test() { addtoarray("zello"); addtoarray("boby"); addtoarray("amy"); addtoarray("coco"); addtoarray("boris"); for(int = 0; < size; i++) { system.out.println(sortedarray[i]); } }
output:
amy boris boby zello coco
here different , more efficient way it.
use arrays.binarysearch()
find proper insertion position. mind you, function returns:
- the index of value if exists.
- the
-(insertposition - 1)
if value doesn't exist.
i assume based on code size
variable represents index of last element in array. means initialize -1 when array empty.
furthermore, think don't allow duplicates , each person
unique solution returns false when try insert duplicate.
of course, make sure resize array after criterium matched (usually when array half full).
public boolean addtoarray(t item) { if (item == null) { return false; } else if (size == -1) { size++; sortedarray[size] = item; return true; } else { // find correct insertion point using binary search int insertionpoint = arrays .binarysearch(sortedarray, 0, size+1, item); if (insertionpoint >= 0) { // duplicate value return false; } // set insertionpoint proper value insertionpoint = (-(insertionpoint) - 1); // shift elements right of insertionpoint (int = size + 1; > insertionpoint; i--) { sortedarray[i] = sortedarray[i - 1]; } // insert @ insertionpoint sortedarray[insertionpoint] = item; // update size size++; return true; //success } }
also, can further simplify compareto(
) method in person
object.
public int compareto(person p) { if(p != null) { return p.age - this.age; } else throw new nullpointerexception(); }
right now, sorting descending. if want make ascending, change:
return p.age - this.age;
to:
return this.age - p.age;
here full running example
example output (with ascending sort):
person [country= uzbekistan, name:john doe, cpr: 200193, age: 21] person [country= america, name:michael jackson, cpr: 112345, age: 49] person [country= denmark, name:john, cpr: 123456, age: 75] person [country= germany, name:pete, cpr: 111111, age: 86]
Comments
Post a Comment