Java examples for Data Structure:Sort
Selection Sort objects
class Date { int year, month, day; Date(int y, int m, int d) { year = y;/*from www. j a va 2 s. c o m*/ month = m; day = d; } static void Display(Date obj) { System.out.println(obj.year + "/" + obj.month + "/" + obj.day); } } public class Main { static boolean GreaterThan(Date a, Date b) { return ((a.year > b.year) || ((a.year == b.year) && (a.month > b.month)) || ((a.year == b.year) && (a.month == b.month) && (a.day > b.day))); } static void swap(Date[] array, int i, int j) { Date tmp = array[i]; array[i] = array[j]; array[j] = tmp; } static void SelectionSort(Date[] array) { int n = array.length; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (GreaterThan(array[i], array[j])) swap(array, i, j); } public static void main(String[] args) { Date[] array = new Date[5]; array[0] = new Date(2008, 06, 01); array[1] = new Date(2005, 04, 03); array[2] = new Date(2005, 05, 27); array[3] = new Date(2005, 04, 01); array[4] = new Date(2005, 04, 15); SelectionSort(array); for (int i = 0; i < array.length; i++) Date.Display(array[i]); System.out.println(""); } }