如何在 Rx 中的 groupBy 之后进行排序

How to sort after a groupBy in Rx

我在一项调查中得到以下回复。想法是按照受欢迎程度的顺序打印标题。因此,我将主题按标题分组,并按照受欢迎程度的顺序打印。我无法弄清楚如何在分组后对它们进行排序。感谢任何帮助。

我的代码

import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
import rx.Observable;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import static java.nio.file.Files.readAllLines;

public class FileProcess
{
    public static Map<Integer, String> LOOKUP = Maps.newConcurrentMap();

    static
    {
        LOOKUP.put(1, "Job Workflow for Connexus using Camel Activiti and the Oracle Grid Engine");
        LOOKUP.put(2, "Observe Don't Bake");
        LOOKUP.put(3, "Model Driven Development with EMF");
        LOOKUP.put(4, "Entity Resolution and Keying");
        LOOKUP.put(5, "Business Activity Monitoring");
        LOOKUP.put(6, "Scorecard Design 101");
        LOOKUP.put(7, "Introduction to Hadoop and Developing for Hadoop");
        LOOKUP.put(8, "Java 7 & 8 New Features");
        LOOKUP.put(9, "A tale of a leaking class loader");
        LOOKUP.put(10, "AOP usecases - Demonstration in.Net with Unity and Postsharp");
        LOOKUP.put(11, "Functional Programming with F#");
        LOOKUP.put(12, "UI Test Automation @ 0/year");
    }

    public static void main(String args[]) throws IOException
    {
        List<String> strings = readAllLines(Paths.get("C:/Users/p0c/Desktop/fav1.txt"));

        Observable.from(strings)
                .filter(line -> !line.endsWith("respondent's answers"))//ignore surveymonkey log
                .map(line -> replaceIndexWithTitle(line))//normalize
                .map(line -> removeIndexAsPrefix(line))//normalize
                .map(line -> line.toLowerCase())
                .groupBy(line -> line)
                .subscribe(observable ->
                                observable.count().subscribe(integer ->
                                                System.out.println(integer + "\t" + observable.getKey())
                                )
                );
    }

    private static String removeIndexAsPrefix(String line)
    {
        if (Character.isDigit(line.trim().charAt(0)))
        {
            return line.trim().substring(line.indexOf(".") + 1);
        }
        return line;
    }

    private static String replaceIndexWithTitle(String line)
    {
        if (Ints.tryParse(line.trim()) != null)
        {
            return LOOKUP.get(Ints.tryParse(line));
        }
        return line;
    }
}

当前输出

1   a tale of a leaking class loader
4   job workflow for connexus using camel, activiti and the oracle grid engine
1   observe don't bake
1   aop usecases - demonstration in .net with unity and postsharp
5   scorecard design 101
15  java 7 & 8 new features
1   job workflow for connexus using camel activiti and the oracle grid engine
16  introduction to hadoop and developing for hadoop
1   business activity monitoring
1   ui test automation @ 100/year
4   model driven development with emf
5   functional programming with f#
1   didn't attend
9   ui test automation @ 0/year
3   entity resolution and keying

示例响应

8
1/14/2015 4:54 PM View respondent's answers
Job Workflow for Connexus using Camel, Activiti and the Oracle Grid Engine
1/14/2015 4:10 PM View respondent's answers
Didn't attend
1/14/2015 11:21 AM View respondent's answers
Java 7 & 8 New Features
1/13/2015 1:17 PM View respondent's answers
functional programming with f#
1/13/2015 1:08 PM View respondent's answers
7.Introduction to Hadoop and Developing for Hadoop
1/13/2015 12:44 PM View respondent's answers
7
1/13/2015 11:11 AM View respondent's answers
6
1/13/2015 10:41 AM View respondent's answers
Java 7 & 8 New Features
1/13/2015 10:38 AM View respondent's answers
Java 7 & 8 New Features
1/13/2015 10:14 AM View respondent's answers
7
1/13/2015 9:56 AM View respondent's answers
UI Test Automation @ 0/year
1/13/2015 9:48 AM View respondent's answers
AOP usecases - Demonstration in .Net with Unity and Postsharp
1/13/2015 9:22 AM View respondent's answers
6.Scorecard Design 101
1/13/2015 9:17 AM View respondent's answers
6
1/13/2015 9:08 AM View respondent's answers
UI Test Automation @ 0/year
1/13/2015 9:06 AM View respondent's answers
4.Entity Resolution and Keying
1/13/2015 9:02 AM View respondent's answers
Java 7 & 8 New Features
1/13/2015 9:01 AM View respondent's answers
Job Workflow for Connexus using Camel, Activiti and the Oracle Grid Engine
1/13/2015 8:57 AM View respondent's answers
6
1/13/2015 8:55 AM View respondent's answers
Job Workflow for Connexus using Camel, Activiti and the Oracle Grid Engine
1/13/2015 8:54 AM View respondent's answers
Java 7 & 8 New Features
1/13/2015 8:54 AM View respondent's answers
7.Introduction to Hadoop and Developing for Hadoop
1/13/2015 8:48 AM View respondent's answers
8
1/13/2015 8:48 AM View respondent's answers
UI Test Automation @ 0/year
1/13/2015 8:47 AM View respondent's answers
6
1/13/2015 8:47 AM View respondent's answers
UI test automation @ 100/year
1/13/2015 8:44 AM View respondent's answers
Model Driven Development with EMF
1/13/2015 8:42 AM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/13/2015 8:42 AM View respondent's answers
12
1/13/2015 8:42 AM View respondent's answers
Java 7 & 8 New Features
1/13/2015 8:37 AM View respondent's answers
1
1/13/2015 7:40 AM View respondent's answers
UI Test Automation @ 0/year
1/12/2015 10:10 AM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/11/2015 9:34 AM View respondent's answers
1.Job Workflow for Connexus using Camel, Activiti and the Oracle Grid Engine
1/8/2015 5:07 PM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/8/2015 2:55 PM View respondent's answers
Java 7 & 8 New Features
1/8/2015 2:09 PM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/8/2015 1:36 PM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/8/2015 1:08 PM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/8/2015 1:02 PM View respondent's answers
UI Test Automation @ 0/year
1/8/2015 12:44 PM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/8/2015 12:31 PM View respondent's answers
UI Test Automation @ 0/year
1/8/2015 12:21 PM View respondent's answers
12.UI Test Automation @ 0/year
1/8/2015 12:04 PM View respondent's answers
8
1/8/2015 12:01 PM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/8/2015 11:58 AM View respondent's answers
Java 7 & 8 New Features
1/8/2015 11:56 AM View respondent's answers
4.Entity Resolution and Keying
1/8/2015 11:56 AM View respondent's answers
7
1/8/2015 11:53 AM View respondent's answers
Model Driven Development with EMF
1/8/2015 11:51 AM View respondent's answers
Introduction to Hadoop and Developing for Hadoop
1/6/2015 11:54 AM View respondent's answers
Java 7 & 8 New Features
1/5/2015 1:14 PM View respondent's answers
3.Model Driven Development with EMF
1/5/2015 11:55 AM View respondent's answers
11.Functional Programming with F#
1/5/2015 11:50 AM View respondent's answers
Java 7 & 8 New Features
1/5/2015 11:44 AM View respondent's answers
Entity Resolution and Keying
1/5/2015 10:26 AM View respondent's answers
Model Driven Development with EMF
1/5/2015 9:54 AM View respondent's answers
11
1/5/2015 9:24 AM View respondent's answers
11
1/5/2015 9:11 AM View respondent's answers
8
1/5/2015 8:44 AM View respondent's answers
7
1/5/2015 8:13 AM View respondent's answers
11
1/5/2015 8:11 AM View respondent's answers
UI Test Automation @ 0/year
1/5/2015 7:22 AM View respondent's answers
7
1/4/2015 4:46 PM View respondent's answers
9
1/4/2015 3:24 PM View respondent's answers
Business Activity Monitoring
1/4/2015 9:43 AM View respondent's answers
Java 7 & 8 New Features
1/3/2015 9:38 PM View respondent's answers
Observe Don't Bake
1/3/2015 3:32 PM View respondent's answers

在 .NET 中,如果它是有限序列,您通常会使用 GroupByUntilGroupBy,并且只需将 ToList 应用于每个组。然后只需交互式排序。我怀疑它在 RxJava.

中是一样的

原因是基于反应数据的排序没有意义。在拥有整个列表之前,您无法对列表进行排序,这意味着您需要 OnCompleted。如果您正在寻找部分排序,那么您可以使用 GroupByUntil 如上所述, until 部分可以定义部分排序边界。

注意,我做出了a PR for a complete set of OrderBy operators in Rx.NET,在上述前提下被审核拒绝了。我在 Twitter 上也与 Erik Meijer 就这个主题进行了简短的讨论,他基本上说服了我,像 OrderBy 这样的有限序列运算符不属于 Rx。我能够确定使用它的潜在性能原因,但最终我无法证明其使用的合理性。

附录:

我想补充一点,如果您的示例显示了您实际使用 Rx 的方式,那么我认为您一开始就不应该在这里使用 Rx。 Rx 用于响应式数据处理,但您只是在处理内存中的列表。在 .NET 中,我们会改用 LINQ to Objects,但我不知道 Java.

中是否有任何等效项