Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created January 26, 2016 14:35
Show Gist options
  • Save Viacheslav77/5c7ff78e54a1ef5108e9 to your computer and use it in GitHub Desktop.
Save Viacheslav77/5c7ff78e54a1ef5108e9 to your computer and use it in GitHub Desktop.
Human
package Human;
public class HumanComparable implements Comparable{
private int age;
public HumanComparable (int age){
this.age= age;
}
public int getAge(){
return age;
}
@Override
public int compareTo (Object another ){
HumanComparable h = (HumanComparable)another;
if(age<h.getAge())
return -1;
if(age>h.getAge())
return 1;
else return 0;
}
}
package Human;
import java.util.Arrays;
public class Myclass {
public static void main(String[] args) {
HumanComparable[] list = { new HumanComparable(40), new HumanComparable(20), new HumanComparable(3), new HumanComparable(7) };
Arrays.sort(list);
System.out.println(list.length + "----------------- " );
HumanComparable[] list1 = new HumanComparable[list.length];
for(int i = list.length-1, j = 0; i >= 0; i--, j++){
list1[j]=list[i];
}
list = list1;
for (HumanComparable h : list)
System.out.println(h.getAge());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment