按照对象数组中某一个属性冒泡排序

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建Student对象
		Student stuObject=new Student();
		//用变量接收对象的createArrays方法生成的对象数组地址值
		Student[] stuObjectArrays=stuObject.createArrays();
		//调用对象的print方法打印对象数组
		stuObject.print(stuObjectArrays);
		//查找班级为3的数据
		stuObject.searchState(stuObjectArrays);
		//按照score排序
		stuObject.sort(stuObjectArrays);
		stuObject.print(stuObjectArrays);
		System.out.println("优化");
	}
}
class Student{
	int number;
	int state;
	int score;
	//创建对象数组,返回数组地址
	Student[] createArrays(){
		Student[] stu=new Student[20];
		for(int i=0;i<20;i++){
			stu[i]=new Student();
			stu[i].number=i+1;
			stu[i].state=(int)(Math.random()*(6-1+1)+1);
			stu[i].score=(int)(Math.random()*101);
		}
		return stu;
	}
	//打印对象数组
	void print(Student[] stu){
		for(int i=0;i<20;i++){
			System.out.println(stu[i].number+" "+stu[i].state
					+" "+stu[i].score);
		}
		System.out.println();
	}
	//查找班级为3的信息
	void searchState(Student[] stu){
		for(int i=0;i<20;i++){
			if(stu[i].state==3)
			System.out.println(stu[i].number+" "+stu[i].state
					+" "+stu[i].score);
		}
		System.out.println();
	}
	//冒泡排序对象数组
	void sort(Student[] stu){
		for(int i=0;i<19;i++)
			for(int j=0;j<stu.length-1-i;j++){
				if(stu[j].score<stu[j+1].score){
					Student temp=new Student();
					temp=stu[j];
					stu[j]=stu[j+1];
					stu[j+1]=temp;
				}
			}
	}
	
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来写一个示例代码,使用冒泡排序实现: ```typescript interface Item { name: string; // 其他属性... } const arr: Item[] = [ { name: 'down' }, { name: 'up' }, { name: 'left' }, { name: 'right' }, { name: 'leftup' }, { name: 'leftdown' }, { name: 'rightup' }, { name: 'rightdown' }, ]; function sortItemsByFirstCharDirection(items: Item[]) { const directions: string[] = ['up', 'down', 'left', 'right', 'leftup', 'leftdown', 'rightup', 'rightdown']; for (let i = 0; i < items.length - 1; i++) { for (let j = 0; j < items.length - i - 1; j++) { const firstChar1 = items[j].name[0]; const firstChar2 = items[j + 1].name[0]; const direction1 = directions.indexOf(firstChar1); const direction2 = directions.indexOf(firstChar2); if (direction1 > direction2) { [items[j], items[j + 1]] = [items[j + 1], items[j]]; } } } return items; } console.log(sortItemsByFirstCharDirection(arr)); // [{name: "up"}, {name: "down"}, {name: "left"}, {name: "right"}, {name: "leftup"}, {name: "leftdown"}, {name: "rightup"}, {name: "rightdown"}] ``` 以上代码,我们先定义了一个 `Item` 接口来表示数组的每个对象,其包含了 `name` 属性。然后,我们定义了一个 `sortItemsByFirstCharDirection` 函数,该函数接收一个 `Item` 类型的数组,并返回按照第一个字符的前后左右上下顺序排列后的数组对象。 在函数内部,我们定义了一个 `directions` 数组,用于存储前后左右上下的顺序。接着,我们使用冒泡排序算法对数组进行排序,比较每个对象的 `name` 属性的第一个字符在 `directions` 数组的索引,如果前面的对象的第一个字符所对应的索引大于后面的对象的第一个字符所对应的索引,就交换它们的位置。 最后,我们调用 `sortItemsByFirstCharDirection` 函数并将数组作为参数传入,打印输出排列后的数组对象
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值