You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何根据Integer类型ages列表对String类型names列表排序

你现有的两个列表索引位置一一对应,即第n个name对应第n个age,所以可以通过对索引按对应age值升序排序,再按排序后的索引提取name得到目标结果,以下是两种实现方式:

基础实现(无高阶语法,适合初学者理解)

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

public class SortByNameAge {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> ages = new ArrayList<>();

        names.add("John");
        names.add("Kate");
        names.add("Bob");

        ages.add(27);
        ages.add(24);
        ages.add(32);

        // 1. 生成对应索引列表
        List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < names.size(); i++) {
            indexList.add(i);
        }

        // 2. 按索引对应的age值升序排序索引
        Collections.sort(indexList, new Comparator<Integer>() {
            @Override
            public int compare(Integer idx1, Integer idx2) {
                return ages.get(idx1) - ages.get(idx2);
            }
        });

        // 3. 按排序后的索引提取name得到结果
        List<String> sortedNames = new ArrayList<>();
        for (int idx : indexList) {
            sortedNames.add(names.get(idx));
        }

        // 输出结果:[Kate, John, Bob]
        System.out.println(sortedNames);
    }
}

Java 8+ 简化流写法

逻辑和基础实现完全一致,语法更简洁:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SortByNameAge {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> ages = new ArrayList<>();

        names.add("John");
        names.add("Kate");
        names.add("Bob");

        ages.add(27);
        ages.add(24);
        ages.add(32);

        List<String> sortedNames = IntStream.range(0, names.size())
                .boxed()
                .sorted(Comparator.comparingInt(ages::get))
                .map(names::get)
                .collect(Collectors.toList());
        
        // 输出结果:[Kate, John, Bob]
        System.out.println(sortedNames);
    }
}

最佳实践建议

实际开发中更推荐把关联属性封装为实体类,避免多列表索引不匹配的潜在问题,示例如下:

// 封装实体类
class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

// 调用逻辑
public class SortTest {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("John", 27));
        personList.add(new Person("Kate", 24));
        personList.add(new Person("Bob", 32));

        // 按age升序排序
        personList.sort(Comparator.comparingInt(Person::getAge));
        // 提取排序后的name列表
        List<String> sortedNames = personList.stream()
                .map(Person::getName)
                .collect(Collectors.toList());
        
        System.out.println(sortedNames);
    }
}

内容的提问来源于stack exchange,提问作者Sukan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 11:21:01