Java中数组和List的排序

1.数组排序

升序排序

直接调用Arrays.sort(Object[] o),传入你想排序的数组即可。
String[] a = {"a", "e", "b", "f", "d", "c"};
Integer[] b = {1, 5, 3, 7, 4, 2, 9};
Arrays.sort(a);
Arrays.sort(b);
以上代码即可实现数组升序排序

降序排序

降序排序需要用到Comparetor接口,调用形式Arrays.sort(Object{] o,new MyComparetor<T>)
 String[] a = {"a", "e", "b", "f", "d", "c"};
 Integer[] b = {1, 5, 3, 7, 4, 2, 9};
                Arrays.sort(a, new Comparator<String>() {
                    @Override
                    public int compare(String s, String t1) {
                        if (s.compareTo(t1) > 0) {
                            return -1;
                        }
                        return 1;
                    }
                });
                Arrays.sort(b, new Comparator<Integer>() {
                    @Override
                    public int compare(Integer integer, Integer t1) {
                        return integer - t1 > 0 ? -1 : 1;
                    }
                });

Comparator接口排序规则:前者某个属性值大于后者属性值,如果返回1,则为升序排序,如果返回-1则为降序排序

2.List排序

list排序主要是通过Comparator接口来实现,主要是通过比较两个对象中的某个属性值的大小并返回正整数、负整数、和0来决定以何种方式来排序list

Comparator接口排序规则:前者某个属性值大于后者属性值,如果返回1,则为升序排序,如果返回-1则为降序排序

下面给出完整的Demo
package code.xzy.com.testsortdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 测试java排序功能
 */
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private static final int ASC = 1;//升序
    private static final int DESC = 2;//降序

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        objectSort(ASC);
        arraySort(ASC);

        objectSort(DESC);
        arraySort(DESC);
    }

    /**
     * java对象按照某一属性排序
     */
    public void objectSort(int type) {
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Student s = new Student();
            s.setId(i + 100);
            list.add(s);
        }

        switch (type) {
            case ASC:
                Collections.sort(list, new Comparator<Student>() {
                    @Override
                    public int compare(Student student, Student t1) {
                        if (student.getId() > t1.getId()) {
                            return 1;
                        }
                        return -1;
                    }
                });
                for (int i = 0; i < list.size(); i++) {
                    Log.i(TAG, "objectSort: asc:" + list.get(i).getId());
                }
                break;
            case DESC:
                Collections.sort(list, new Comparator<Student>() {
                    @Override
                    public int compare(Student student, Student t1) {
                        if (student.getId() > t1.getId()) {
                            return -1;
                        }
                        return 1;
                    }
                });
                for (int i = 0; i < list.size(); i++) {
                    Log.i(TAG, "objectSort: desc:" + list.get(i).getId());
                }
                break;
        }
    }

    /**
     * 数组排序
     */
    public void arraySort(int type) {
        String[] a = {"a", "e", "b", "f", "d", "c"};
        Integer[] b = {1, 5, 3, 7, 4, 2, 9};
        switch (type) {
            case ASC:
                Log.i(TAG, "arraySort: 升序排序:");

                Arrays.sort(a);
                Arrays.sort(b);

                for (int i = 0; i < a.length; i++) {
                    Log.i(TAG, "arraySort: asc a:" + a[i]);
                    //结果为 a b c d e f
                }
                for (int i = 0; i < b.length; i++) {
                    Log.i(TAG, "arraySort: asc b:" + b[i]);
                    //结果为 1 2 3 4 5 7 9
                }
                break;
            case DESC:
                Log.i(TAG, "arraySort: 降序排序:");
                Arrays.sort(a, new Comparator<String>() {
                    @Override
                    public int compare(String s, String t1) {
                        if (s.compareTo(t1) > 0) {
                            return -1;
                        }
                        return 1;
                    }
                });
                Arrays.sort(b, new Comparator<Integer>() {
                    @Override
                    public int compare(Integer integer, Integer t1) {
                        return integer - t1 > 0 ? -1 : 1;
                    }
                });
                for (int i = 0; i < a.length; i++) {
                    Log.i(TAG, "arraySort: desc a:" + a[i]);
                    //结果为  f e d c b a
                }
                for (int i = 0; i < b.length; i++) {
                    Log.i(TAG, "arraySort: desc b:" + b[i]);
                    //结果为 9 7 5 4 3 2 1
                }
                break;
        }
    }
}
蛮简单的用法,稍稍总结下。

发布了331 篇原创文章 · 获赞 167 · 访问量 95万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览