Java中从最小到最大到最小的排序列表 [英] Sorting list from smallest-largest-smallest in Java

查看:215
本文介绍了Java中从最小到最大到最小的排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对一组数据进行排序,以使其看起来像是概率分布函数的直方图(我现在假设是正态分布的。)

I'm trying to sort a set of data so that it looks like a histogram of a probability distribution function (I'm assuming normally distributed for the moment).

我有一个条目列表:

private static final class SortableDatasetEntry{
    Number value;
    Comparable key;
    public SortableDatasetEntry(Number value, Comparable key){
      this.value = value;
      this.key = key;
    }
}

例如:
我有物品: {1,2,3,4,5,6,7,8,9}

编辑:
我想要的排序列表: {1,3,5,7,9,8,6,4,2} (或类似的东西)数字不会总是那么整洁(即简单地按奇/偶排序也不行)。我有一个部分解决方案,涉及按规则顺序排序(从最低到最高),然后每次通过插入到中间将列表复制到另一个列表,因此最后插入(到中间)的项最大。我仍然想找到一种使用比较器执行此操作的方法。

The sorted list I would like: {1,3,5,7,9,8,6,4,2} (or something similar) The numbers will not always be so neat (i.e. simply sorting by odd/even wont work either). I have a partial solution that involves sorting by regular order (lowest to highest) then copying that list to another by inserting into the middle each time, thus the last item inserted (into the middle) is the largest. I'd still like to find a method of doing this with a comparator.

这很棘手,因为它没有按<$的绝对值排序c $ c> value ,但与集合中的Mean( value )的距离,然后以某种方式移动,因此最接近均值的那些值是居中。我知道compareTo函数必须是可逆的(我忘记了正确的术语)。

This is quite tricky because its not being sorted by the absolute value of value but by the distance from the Mean(value) within the set, and then somehow moved so those values closest to mean are centered. I know that the compareTo function must be "reversible" (I forget the correct term).

奖励积分:如何确定数据(即,假设不正常)。

Bonus points: How do I determine the correct distribution for the data (i.e. if it isn't normal, as assumed).

推荐答案

首先计算均值并将其存储在名为说平均值。接下来,将条目插入SortableDatasetEntry时,使用 value-mean 作为每个条目的实际值,而不是 value

First calculate the mean and store it in a variable called say mean. Next, when you insert the entries into your SortableDatasetEntry, use value - mean as the actual value for each entry rather than value.

这篇关于Java中从最小到最大到最小的排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆