Created
January 26, 2016 14:35
-
-
Save Viacheslav77/5c7ff78e54a1ef5108e9 to your computer and use it in GitHub Desktop.
Human
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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