比较器使用nullsFirst对用户定义的对象进行排序

时间:2019-12-09 17:55:10

标签: java jsp collections comparator

我在向量中有以下列表

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          backgroundColor: Colors.teal,
          body: SafeArea(
            child: Row(
              children: <Widget>[
                SizedBox(width: 15),
                Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Card(
                      color: Colors.white,
                      child: Center(
                        child: Padding(
                          padding: EdgeInsets.all(75.0),
                          child: Text(
                            '1',
                            style: TextStyle(fontSize: 40, color: Colors.black),
                          ),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      child: Center(
                        child: Padding(
                          padding: EdgeInsets.all(75.0),
                          child: Text(
                            '3',
                            style: TextStyle(fontSize: 40, color: Colors.black),
                          ),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      child: Center(
                        child: Padding(
                          padding: EdgeInsets.all(75.0),
                          child: Text(
                            '5',
                            style: TextStyle(fontSize: 40, color: Colors.black),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(width: 15),
                Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Card(
                      color: Colors.white,
                      child: Center(
                        child: Padding(
                          padding: EdgeInsets.all(75.0),
                          child: Text(
                            '2',
                            style: TextStyle(fontSize: 40, color: Colors.black),
                          ),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      child: Center(
                        child: Padding(
                          padding: EdgeInsets.all(75.0),
                          child: Text(
                            '4',
                            style: TextStyle(fontSize: 40, color: Colors.black),
                          ),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      child: Center(
                        child: Padding(
                          padding: EdgeInsets.all(75.0),
                          child: Text(
                            '6',
                            style: TextStyle(fontSize: 40, color: Colors.black),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )),
    );
  }
}

我正在用Websphere 9服务器在jsp中调用此比较器。 Java 1.8 Lambda表达式给我错误,并要求安装补丁。

所以我要使用没有lambda表达式的传统比较器。

Class abc{
     String view;
     String viewDesc;
}

Data: 
viewDesc = null
viewDesc = null
viewDesc = Maint
viewDesc = Maint
viewDesc = null
viewDesc = uncategorized
viewDesc = uncategorized
viewDesc = null
viewDesc = null
viewDesc = Maint

I want to sort nullsFirst then alphabetically in reverse order as fallows
viewDesc = null
viewDesc = null
viewDesc = null
viewDesc = null
viewDesc = uncategorized
viewDesc = uncategorized
viewDesc = uncategorized
viewDesc = Maint
viewDesc = Maint
viewDesc = Maint

public class SortbyViewComparator implements Comparator<abc> {

    public int compare(abc object1, abc object2)
    {
        return object1.getViewDesc().compareToIgnoreCase(object2.getViewDesc ());
    }
}

这给了我以下错误

此接口Comparator的静态方法只能作为Comparator.nullsFirst

访问

@deadPool     该解决方案正在紧密合作。我至少没有出错。
    现在我按此顺序。实际上在代码中,如果它为null,那么我会将null附加到未分类的。     因此,按未分类,null就是什么。     viewDesc = null     viewDesc = null     viewDesc = null     viewDesc = null
    viewDesc = Maint
    viewDesc =维护     viewDesc =维护     viewDesc =未分类     viewDesc =未分类     viewDesc =未分类

我想要这样的输出。所有null和未分类的内容,首先是Maint,HAP,PPA等

JSP code:
Vector abcList;

SortbyViewComparator comp = new SortbyViewComparator();         
Collections.sort(abcList, Comparator.nullsFirst(comp));

2 个答案:

答案 0 :(得分:1)

如果您使用的是Java-7,则可以编写自定义Comparator来对null进行排序,然后再按字母相反的顺序进行排序

public class SortbyViewComparator implements Comparator<abc> {

   public int compare(abc object1, abc object2) {

      if (object1.getViewDesc() == null && object2.getViewDesc() == null) {
            return 0;

        } else if (object1.getViewDesc() == null) {
            return -1;

        } else if (object2.getViewDesc() == null) {
            return 1;
        }

        return object1.getViewDesc().compareTo(object2.getViewDesc());
   }
}

但是在Java-8中,您可以简单地使用Comparator静态方法

Comparator.nullsFirst(Comparator.comparing(abc::getViewDesc).reversed())

答案 1 :(得分:-1)

使用方式

Comparator.nullsFirst(Comparator.comparing(abc::getViewDesc));