简体   繁体   English

排序不同数据类型的多个数组而不使用对象

[英]sorting multiple arrays of different data types without using objects

I am asked in an assignment in java course to sort multiple arrays according to the Average of the student 我在java课程的作业中被要求根据学生的平均值对多个数组进行排序

    201302972, AhmadJouni, 82 , 85 , 89 , Average: 85.333336
    201303123, Joe, 70 , 75 , 96 , Average: 80.333336
    201401034, Mohamad, 90 , 54 , 49 , Average: 64.333336
    201402100, Ali, 80 , 90 , 99 , Average: 89.666664

My arrays : 我的阵列:

int ID[] = new int [4];
int Grades[][]= new int [4][3];
String Name[] = new String[4];

I have to sort all of the above according to averages in increasing order 我必须按照递增顺序的平均值对上述所有内容进行排序

Final output should look like 最终输出应该是这样的

        201401034, John, 90 , 54 , 49 , Average: 64.333336
        201303123, Joe, 70 , 75 , 96 , Average: 80.333336
        201302972, Mike, 82 , 85 , 89 , Average: 85.333336
        201402100, Amy, 80 , 90 , 99 , Average: 89.666664

Note : The use of objects/constructors is not allowed , should be done in some simple way. 注意:不允许使用对象/构造函数,应该以一些简单的方式完成。 This is the challenge 这是挑战

Full code : http://textuploader.com/1he1 完整代码: http//textuploader.com/1he1

您可以使用与使用单个字段相同的方式执行此操作,但不是在一个数组中交换值,而是一次交换所有数组中相同位置的值。

Create one additional array which contains the integer indices of your data arrays. 创建一个包含数据数组的整数索引的附加数组。 Then sort the indices rather than the data arrays. 然后对索引而不是数据数组进行排序。 By adding multiple index arrays, you could even have multiple sort orders based on different criteria without ever moving the original data. 通过添加多个索引数组,您甚至可以根据不同的条件拥有多个排序顺序,而无需移动原始数据。

The sort would order the index set using pseudo-code logic along the lines of: 排序将使用伪代码逻辑对索引集进行排序:

if (average[index[i]] > average[index[j]]) {
    swap(index[i], index[j])
}

It's not clear from the restriction you cited whether you are supposed to write your own sort or can create a Comparator, but the fundamental idea remains the same: that you rearrange the index set rather than the much more expensive operation of moving multiple arrays worth of data synchronously. 从你所引用的限制中不清楚你是应该编写自己的排序还是可以创建一个比较器,但基本思想仍然是相同的:你重新排列索引集而不是更昂贵的移动多个数组的操作数据同步。

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

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