简体   繁体   English

根据计算值对数组进行排序

[英]Sorting an array based on calculated values

For a recent java coding test I had a question similar to the following:对于最近的 Java 编码测试,我有一个类似于以下内容的问题:

A function receives a string of comma separated values.函数接收一串逗号分隔值。 Add the digits of each number contained to create a single value for that number.添加包含的每个数字的数字以创建该数字的单个值。 Order these in ascending order Return the original values (ie not the calculated ones) in this sorted order.按升序对这些进行排序 按此排序顺序返回原始值(即不是计算值)。

Hence if the string input is:因此,如果字符串输入是:

{"91,15,20"} The digits after adding would be: 10, 6, 2 since (9+1 == 10, 1+5 == 6, 2+0 == 2) {"91,15,20"} 相加后的数字为: 10, 6, 2 因为 (9+1 == 10, 1+5 == 6, 2+0 == 2)

After sorting into ascending order: 2,6,10升序排序后:2,6,10

We would thus return the original string sorted as above as as: 20,15,91因此,我们将返回按上述方式排序的原始字符串:20,15,91

From a coding perspective this means we would split the string on the comma into a string array从编码的角度来看,这意味着我们会将逗号上的字符串拆分为一个字符串数组

For each item, convert the string to an int and then calculate its 'value'对于每个项目,将字符串转换为 int,然后计算其“值”

Now, this is where I'm foxed!现在,这就是我被骗的地方!

At first I added the calculated item to an array list and then sorted it but realised that since I needed to return the original strings (not the calculated values), this was no good.起初我将计算项添加到数组列表中,然后对其进行排序,但意识到由于我需要返回原始字符串(而不是计算值),这并不好。

As such I'm looking for advise for future knowledge.因此,我正在寻找有关未来知识的建议。

I don't actually specifically want code but more so advice on the approach.我实际上并不是特别想要代码,而是更想要关于方法的建议。

Would using a map have been the best way?使用地图会是最好的方法吗? I contemplated that but can't see how that work, especially if there could be duplicate values in the string.我考虑过,但看不到它是如何工作的,特别是如果字符串中可能存在重复值。 Would it have been better to have two arrays?有两个数组会更好吗? Is it best to sort as I parse the string array?最好在解析字符串数组时进行排序吗?

Although I've failed that question I want to still complete it so that I'll know.. I need to learn from failure after all.虽然我没有通过那道题,但我仍然想完成它,以便我知道..毕竟我需要从失败中学习。

Would be grateful for advice in the best approach please.请以最佳方法提供建议将不胜感激。

Here is some advice.这里有一些建议。 I assume you are using Java 8.我假设您使用的是 Java 8。

Instead of putting your values in a data structure like a map or something, why not discarding them after the calculation, if you don't care about performance?如果您不关心性能,与其将您的值放在像地图之类的数据结构中,为什么不在计算后丢弃它们?

Since JDK 1.8, there is this sort method on ArrayList that accepts a comparator.从 JDK 1.8 开始, ArrayList这种接受比较器的sort方法。 A Comparator<Integer> has this method: Comparator<Integer>有这个方法:

public int compare(Integer o1, Integer o2) {
    
}

If you just fill in this method with something like this...如果你只是用这样的东西填写这个方法......

public int compare(Integer o1, Integer o2) {
    Integer a = // add up the digits in o1
    Integer b = // add up the digits in o2
    return a.compareTo(b);
}

The sort will work!排序会起作用! You can then prettify your code by turning this into a lambda:然后,您可以通过将其转换为 lambda 来美化您的代码:

list.sort(
        (o1, o2) -> {
                Integer a = <calculation>;
                Integer b = <calculation>;
                return a.compareTo(b);
        }
);

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

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