简体   繁体   English

根据另一个数组对数组排序

[英]Sorting a Array based on another array

I have 2 arrays : 我有2个数组:

The first one consists of names of persons . 第一个由人名组成。 The second one gives me a count of number of names that have appeared in a page. 第二个给了我一个页面上出现的名字数量的计数。 Ex: Names=["James","Loiui","Mario","Peter"] count=[1,4,2,5] 例如:姓名= [“詹姆斯”,“路易伊”,“马里奥”,“彼得”] count = [1、4、2、5]

now I tried with this code give below : 现在我尝试使用下面的代码:

SortedMap<Integer, String> m = new TreeMap<Integer, String>();
for(int i = 0; i < Names.size(); i++)
        m.put( count.get(i),Names.get(i));

But this doesnt work given there are repeating values of count . 但是,鉴于存在重复的count值,这不起作用。 I figured out that the problem is with TreeMap as it stores only unique elements . 我发现问题出在TreeMap上,因为它仅存储唯一元素。 Now to overcome problem of mine, are there any other valid solutions . 现在要克服我的问题,还有其他有效的解决方案。

Create a class PersonFrequency , containing a name and a count. 创建一个PersonFrequency类,其中包含一个名称和一个计数。 Create a single array or list of PersonFrequency instances. 创建单个数组或PersonFrequency实例列表。 Sort this array by count: 按计数对该数组排序:

Java 8 example: Java 8示例:

List<PersonFrequency> list = new ArrayList<>(names.length);
for (int i = 0; i < names.length; i++) {
    list.add(new PersonFrequency(names[i], counts[i]);
}

list.sort(Comparator.comparing(PersonFrequency::getCount).reversed());

List<String> sortedNames = list.stream()
                               .map(PersonFrequency::getName)
                               .collect(Collectors.toList());

In Java 7, the sort would become 在Java 7中,排序将变为

Collections.sort(list, new Comparator<PersonFrequency>() {
    @Override
    public int compare(PersonFrequency p1, PersonFrequency p2) {
        return Integer.compare(p2.getCount(), p1.getCount());
    }
});

Sorted alphabetically 按字母顺序排序

    String [] names = {"Aaa","Bbb"};
    int [] count = {1,2};

    SortedMap<String, Integer> m = new TreeMap<String, Integer>();

    for(int i = 0; i < names.length; i++){
        m.put(names[i], count[i]);
    }

Sorted by number: 按数字排序:

    String [] names = {"Aaa","Bbb"};
    int [] count = {2,1};

    SortedMap<Integer, String> m = new TreeMap<Integer, String>();

    for(int i = 0; i < names.length; i++){
        m.put(count[i],names[i]);
    }

For descending change your sorted map to this 对于降序,将已排序的地图更改为此

SortedMap<Integer, String> m = new TreeMap<Integer, String>().descendingMap();

As mentioned in the previous comments you can have a sorted map with Integer and ArrayList parameters as below, 正如前面的评论中提到的,您可以使用Integer和ArrayList参数对映射进行排序,如下所示,

SortedMap<Integer, ArrayList<String>> map=new TreeMap<Integer, ArrayList<String>>().descendingMap();

Since you want the results to be in descending order I added descendingMap(). 由于您希望结果按降序排列,因此我添加了descendingMap()。

We use ArrayList because you may have duplicates in count array. 我们使用ArrayList,因为您可能在count数组中有重复项。 More than one name can have the same count value. 多个名称可以具有相同的计数值。 So one count will have a list of names. 因此,一个计数将具有一个名称列表。

And the complete code is below, 完整的代码如下

import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;



public class NameCount {

    public static void main(String[] args) {

        String[] names = {"James","Loiui","Mario","Peter"};
        int[] count = {1, 4, 2, 1};

        SortedMap<Integer, ArrayList<String>> map=new TreeMap<Integer, ArrayList<String>>().descendingMap();
        ArrayList<String> nameList=null;

        for(int i = 0; i < names.length; i++)
        {
            if(map.get(count[i])==null)
            {
                nameList = new ArrayList<String>();
                nameList.add(names[i]);
                map.put(count[i], nameList);
            }
            else
            {
                map.get(count[i]).add(names[i]);
            }
        }

        for(int countVal : map.keySet())
        {
            ArrayList<String> namesListVal=map.get(countVal);

            System.out.print("\nCount  "+countVal+ ":  NAMES  : ");
            for(String name : namesListVal)
            {
                System.out.print(name+"  ");
            }

        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM