如何根据存储的 Double 分数对字符串列表进行排序?

huangapple go评论73阅读模式
英文:

How to sort a List of String according to a Double score that is stored?

问题

这是我的问题:我必须显示一组嵌套结构,并且我有一个问题。我应该将一些查询写入文件,带有关联的结果文件,查询在文件中出现的次数,以及计算出的分数。唯一的问题是我必须按分数而不是按字母顺序对显示的结果进行排序(这是我现在正在做的)。我不知道该使用什么或从哪里开始,所以如果我能得到一些帮助,我会非常高兴。这是函数:

public static void asNestedArrayQueries(List<String> elements, TreeMap<String, TreeMap<String, TreeSet<Integer>>> actual, TreeMap<String, Integer> countMap, Writer writer, int level)
		throws IOException {
	TreeMap<String, Integer> resultMap = new TreeMap<String, Integer>();
	int counter = 0, counterOfFiles = 0;
	double divider = 0;
	elements.sort(Comparator.comparing(String::toString));
	writer.write("{\n");
	for (int i = 0; i < elements.size(); i++) {
		counter++;
		indent(writer, 1);
		writer.write("\"" + elements.get(i) + "\": [\n");
		String[] words = elements.get(i).split(" ");
		counterOfFiles = 0;
		for (int j = 0; j < words.length; j++) {
			if (actual.containsKey(words[j])) {
				for (String keyTwo : actual.get(words[j]).keySet()) {
					if (!resultMap.containsKey(keyTwo)) {
						resultMap.put(keyTwo, actual.get(words[j]).get(keyTwo).size());
					} else if (resultMap.containsKey(keyTwo)) {
						resultMap.put(keyTwo, resultMap.get(keyTwo) + actual.get(words[j]).get(keyTwo).size());
					}
					System.out.println(resultMap);
					divider = countMap.get(keyTwo);
				}
			}
		}
		for (String key : resultMap.keySet()) {
			double score = resultMap.get(key) / divider;
			String formatted;
			counterOfFiles++;
			indent(writer, 2);
			writer.write("{\n");
			indent(writer, 3);
			writer.write("\"where\": \"" + key + "\",\n");
			indent(writer, 3);
			writer.write("\"count\": " + resultMap.get(key) + ",\n");
			indent(writer, 3);
			formatted = String.format(Locale.US, "%.8f", score);
			writer.write("\"score\": " + formatted + "\n");
			if (counterOfFiles < resultMap.keySet().size()) {
				indent(writer, 2);
				writer.write("},\n");
			} else {
				indent(writer, 2);
				writer.write("}\n");
			}
		}
		if (counter < elements.size()) {
			indent(writer, 1);
			writer.write("],\n");
		} else {
			indent(writer, 1);
			writer.write("]\n");
		}
		resultMap.clear();
	}
	writer.write("}");
	elements.clear();
}
英文:

Here is my problem: I have to display a set of Nested structures and I have a problem. I am supposed to write to a file a queries, with associated result file, number of time the query appears in the file, and a score that is calculated. Only problem is that I have to sort the displayed result by score and not by alphabetical order ( which is what I am doing right now ). I have no idea what to use or where to start so I'd be incredibly happy if I could get some help. Here is the function:

public static void asNestedArrayQueries(List&lt;String&gt; elements, TreeMap&lt;String, TreeMap&lt;String, TreeSet&lt;Integer&gt;&gt;&gt; actual, TreeMap&lt;String, Integer&gt; countMap, Writer writer, int level)
throws IOException {
TreeMap&lt;String, Integer&gt; resultMap = new TreeMap&lt; String, Integer&gt;();
int counter=0, counterOfFiles=0;
double divider=0;
elements.sort( Comparator.comparing( String::toString ) ); 
writer.write(&quot;{\n&quot;);
for(int i=0;i&lt;elements.size();i++) {
counter++;
indent(writer, 1);
writer.write(&quot;\&quot;&quot; + elements.get(i) + &quot;\&quot;: [\n&quot;);	
String[] words = elements.get(i).split(&quot; &quot;);
counterOfFiles=0;
for(int j=0;j&lt;words.length;j++) {
if(actual.containsKey(words[j])){
for(String keyTwo : actual.get(words[j]).keySet()) {
if(!resultMap.containsKey(keyTwo)) {
resultMap.put(keyTwo, actual.get(words[j]).get(keyTwo).size());
}
else if(resultMap.containsKey(keyTwo)){
resultMap.put(keyTwo, resultMap.get(keyTwo)+actual.get(words[j]).get(keyTwo).size());
}
System.out.println(resultMap);
divider = countMap.get(keyTwo);
}
}
}
for(String key : resultMap.keySet()) {
double score = resultMap.get(key)/divider;
String formatted;
counterOfFiles++;
indent(writer, 2);
writer.write(&quot;{\n&quot;);
indent(writer, 3);
writer.write(&quot;\&quot;where\&quot;: \&quot;&quot; + key + &quot;\&quot;,\n&quot;);
indent(writer, 3);
writer.write(&quot;\&quot;count\&quot;: &quot; + resultMap.get(key) + &quot;,\n&quot;);
indent(writer, 3);
formatted = String.format(Locale.US, &quot;%.8f&quot;, score);
writer.write(&quot;\&quot;score\&quot;: &quot; + formatted + &quot;\n&quot;);
if(counterOfFiles&lt;resultMap.keySet().size()) {
indent(writer, 2);
writer.write(&quot;},\n&quot;);
}
else {
indent(writer, 2);
writer.write(&quot;}\n&quot;);
}
}
if(counter&lt;elements.size()) {
indent(writer, 1);
writer.write(&quot;],\n&quot;);
}
else {
indent(writer, 1);
writer.write(&quot;]\n&quot;);
}
resultMap.clear();
}
writer.write(&quot;}&quot;);
elements.clear();
}

答案1

得分: 0

我正在对Map进行排序,但也适用于TreeMap。您可以通过创建自定义比较器按值对其进行排序 -> Collections.sort(list, customComparator)。
这里有一个简单Map的示例,其中包含一个键和一个值。是的,这里使用的值是String,但是对于您的分数,也可以是Integer:

Map<Integer, String> firstMap = new ConcurrentHashMap<>();
Map<Integer, String> secondMap = new ConcurrentHashMap<>();
firstMap.put(1, "name");
firstMap.put(2, "year");
firstMap.put(3, "month");
firstMap.put(4, "day");

secondMap = sortByComparator(firstMap, true);

该方法:

/**
 * 此方法按顺序对ConcurrentHashMap变量进行排序
 * @param unsortedMap - 要排序的ConcurrentHashMap
 * @param order       - true表示降序,false表示升序
 * @return 排序后的ConcurrentHashMap变量
 */
private static Map<Integer, String> sortByComparator(Map<Integer, String> unsortedMap, final boolean order) {
    List<Map.Entry<Integer, String>> list = new LinkedList<Map.Entry<Integer, String>>(unsortedMap.entrySet());

    // 基于值通过自定义比较器对列表进行排序
    Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
        public int compare(Map.Entry<Integer, String> o1,
                           Map.Entry<Integer, String> o2) {
            if (order) {
                return o1.getValue().compareTo(o2.getValue());
            } else {
                return o2.getValue().compareTo(o1.getValue());
            }
        }
    });

    // 借助LinkedList维护插入顺序
    Map<Integer, String> sortedMap = new LinkedHashMap<Integer, String>();
    for (Map.Entry<Integer, String> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}
英文:

I am sorting Map but it may work for TreeMap as well. You can sort it by value via creating custom Comparator -> Collections.sort(list, customComparator) .
Here is example for a simple Map with one Key and one Value. Yes the Value used here is String, but it can be Integer as well for your score:

Map&lt;Integer, String&gt; firstMap = new ConcurrentHashMap&lt;&gt;();
Map&lt;Integer, String&gt; secondMap = new ConcurrentHashMap&lt;&gt;();
firstMap.put(1, &quot;name&quot;);
firstMap.put(2, &quot;year&quot;);
firstMap.put(3, &quot;month&quot;);
firstMap.put(4, &quot;day&quot;);
secondMap = sortByComparator(firstMap, true);

the method:

 /**
* This method sorts ConcurrentHashMap variable by order
* @param unsortedMap - the ConcurrentHashMap to sort
* @param order       - true for descending order, false for ascending
* @return sorted ConcurrentHashMap variable
*/
private static Map&lt;Integer, String&gt; sortByComparator(Map&lt;Integer, String&gt; unsortedMap, final boolean order) {
List&lt;Map.Entry&lt;Integer, String&gt;&gt; list = new LinkedList&lt;Map.Entry&lt;Integer, String&gt;&gt;(unsortedMap.entrySet());
// Sorting the list based on values via CUSTOM COMPARATOR
Collections.sort(list, new Comparator&lt;Map.Entry&lt;Integer, String&gt;&gt;() {
public int compare(Map.Entry&lt;Integer, String&gt; o1,
Map.Entry&lt;Integer, String&gt; o2) {
if (order) {
return o1.getValue().compareTo(o2.getValue());
} else {
return o2.getValue().compareTo(o1.getValue());
}
}
});
// Maintaining insertion order with the help of LinkedList
Map&lt;Integer, String&gt; sortedMap = new LinkedHashMap&lt;Integer, String&gt;();
for (Map.Entry&lt;Integer, String&gt; entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}

huangapple
  • 本文由 发表于 2020年10月15日 17:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64368223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定