package collection.sort.comparable;
import java.util.Date;
/**
* 内置引用数据类型(常用)的比较
*/
public class _1_InnerTypeCompare {
public static void main(String[] args) {
@SuppressWarnings("unused")
Integer a ;
@SuppressWarnings("unused")
Character ch;
String str="abc";
String str2 ="abcd123";
System.out.println(str.compareTo(str2));
str ="abc";
str2 ="aad";
System.out.println(str.compareTo(str2));
@SuppressWarnings("unused")
Date d ;
}
}
package collection.sort.comparable;
import java.util.Arrays;
public class _2_StringArraySort {
public static void main(String[] args) {
String[] arr ={"a","abcd","abc","def"};
boolean sorted= true;
int len =arr.length;
for(int j=0;j<len-1;j++){
sorted =true;
for(int i=0;i<len-1-j;i++){
if((arr[i]).compareTo(arr[i+1])<0){
String temp = arr[i];
arr[i] =arr[i+1];
arr[i+1] =temp;
sorted =false;
}
}
if(sorted){
break;
}
}
System.out.println(Arrays.toString(arr));
}
}
package collection.sort.comparable;
import java.util.Arrays;
import java.util.Date;
public class _3_DateArraySort {
public static void main(String[] args) {
Date arr[] =new Date[3];
arr[0] =new Date();
arr[1] =new Date(System.currentTimeMillis()-1000*60*60);
arr[2] =new Date(System.currentTimeMillis()+1000*60*60);
boolean sorted= true;
int len =arr.length;
for(int j=0;j<len-1;j++){
sorted =true;
for(int i=0;i<len-1-j;i++){
if((arr[i]).compareTo(arr[i+1])<0){
Date temp = arr[i];
arr[i] =arr[i+1];
arr[i+1] =temp;
sorted =false;
}
}
if(sorted){
break;
}
}
System.out.println(Arrays.toString(arr));
}
}
package collection.sort.comparable;
import java.util.List;
public class CompareUtils {
/**
* 数组的排序 (降序)
* @param arr
*/
@SuppressWarnings("unchecked")
public static void sort(Object[] arr){
boolean sorted= true;
int len =arr.length;
for(int j=0;j<len-1;j++){
sorted =true;
for(int i=0;i<len-1-j;i++){
if(((Comparable<Object>)arr[i]).compareTo(arr[i+1])<0){
Object temp = arr[i];
arr[i] =arr[i+1];
arr[i+1] =temp;
sorted =false;
}
}
if(sorted){
break;
}
}
}
/**
* 数组降序排序 (使用泛型方法)
*/
public static <T extends Comparable<T>> void sort(T[] arr){
boolean sorted= true;
int len =arr.length;
for(int j=0;j<len-1;j++){
sorted =true;
for(int i=0;i<len-1-j;i++){
if((arr[i]).compareTo(arr[i+1])<0){
T temp = arr[i];
arr[i] =arr[i+1];
arr[i+1] =temp;
sorted =false;
}
}
if(sorted){
break;
}
}
}
/**
* 容器排序 (使用泛型方法)
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<T>> void sort(List<T> list){
Object[] arr =list.toArray();
sort(arr);
for(int i=0;i<arr.length;i++){
list.set(i, (T)(arr[i]));
}
}
}
package collection.sort.comparable
import java.util.ArrayList
import java.util.Arrays
import java.util.Date
import java.util.List
public class ComparableImpl {
public static void main(String[] args) {
//时间
Date[] arr =new Date[3]
arr[0] =new Date()
arr[1] =new Date(System.currentTimeMillis()-1000*60*60)
arr[2] =new Date(System.currentTimeMillis()+1000*60*60)
CompareUtils.sort(arr)
System.out.println(Arrays.toString(arr))
//字符串
String[] arr2 ={"a","abcd","abc","def"}
CompareUtils.sort(arr2)
System.out.println(Arrays.toString(arr2))
System.out.println("==========List排序===========")
//存放容器中
List<String> list =new ArrayList<String>()
list.add("a")
list.add("abcd")
list.add("abc")
list.add("def")
CompareUtils.sort(list)
System.out.println(list)
}
}