Java实现对象数组的按某个元素排序

如题,本文为笔者的笔记 希望能帮到你

package com.mxy.homework;
import java.util.Scanner;

/**
 * @author MXHstrat
 * @create 2021 - 11 - 02  19:05
 */
public class CustomerTest02 {
    public static void main(String[] args) {
        CustomerHandle c1 = new CustomerHandle();

        do {
            Customer02 cust02 = new Customer02();

            System.out.println("添加客户的姓名:");
            cust02.name = new Scanner(System.in).next();
            System.out.println("添加客户的年龄:");
            cust02.age = new Scanner(System.in).nextInt();
            System.out.println("添加客户的积分:");
            cust02.integral = new Scanner(System.in).nextInt();

            c1.add(cust02);

            System.out.println("是否继续再添加一个客户信息(y/n)");
        } while ("Y".equalsIgnoreCase(new Scanner(System.in).next()));
        c1.showAll();

        System.out.println("按积分进行升序排列为:");
        c1.ascSort();
        System.out.println("按积分进行降序排列为:");
        c1.descSort();

    }

}

class CustomerHandle {
    Customer02[] cust = new Customer02[1000];

    public void add(Customer02 customer02s) {
        for (int i = 0; i < cust.length; i++) {
            if (cust[i] == null) {
                cust[i] = customer02s;
                break;
            }
        }
    }

    public void showAll() {
        System.out.println("名字\t" + "\t年龄\t" + "\t积分");
        System.out.println("-----------------------------");
        for (Customer02 abc : cust) {
            if (abc != null) {
                abc.getShow();
            }
        }
        System.out.println("-----------------------------");
    }

    public void ascSort() {
            for (int i = 0; i < cust.length; i++) {
                for (int j = 0; j < cust.length - 1 - i; j++) {
                    if (cust[j+1] != null) {
                    if (cust[j].integral > cust[j + 1].integral) {
                        Customer02 temp = cust[j];
                        cust[j] = cust[j + 1];
                        cust[j + 1] = temp;
                    }
                }
            }
        }
        System.out.println("名字\t" + "\t年龄\t" + "\t积分");
        System.out.println("-----------------------------");
        for (Customer02 abc : cust) {
            if (abc != null) {
                abc.getShow();
            }
        }
        System.out.println("-----------------------------");
    }

    public void descSort() {
        for (int i = 0; i < cust.length; i++) {
            for (int j = 0; j < cust.length - 1 - i; j++) {
                if (cust[j+1] != null) {
                    if (cust[j].integral < cust[j + 1].integral) {
                        Customer02 temp = cust[j];
                        cust[j] = cust[j + 1];
                        cust[j + 1] = temp;
                    }
                }
            }
        }
        System.out.println("名字\t" + "\t年龄\t" + "\t积分");
        System.out.println("-----------------------------");
        for (Customer02 abc : cust) {
            if (abc != null) {
                abc.getShow();
            }
        }
        System.out.println("-----------------------------");
    }

}

------------------------------------------------------分割线------------------------------------------------------

package com.mxy.homework;

/**
 * @author MXHstrat
 * @create 2021 - 11 - 02  19:07
 */
public class Customer02 {
    String name;
    int age;
    int integral;

    public void getShow(){
        System.out.println(name +"\t"+age+"\t"+"\t"+integral);
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值