字符串根据长度排序,然后根据大小写排序

时间:2019-02-19 14:54:34

标签: java string

我正在尝试对String列表进行排序,该列表根据长度和区分大小写进行排序。

示例: 排序前:[a,abc,b,fe,e,ABC,Abc]

排序后:[a,b,e,fe,abc,Abc,ABc,ABC]

public static void main(String [] args){

    List<String> stringList = new ArrayList<>();
    stringList.add("a");
    stringList.add("abc");
    stringList.add("b");
    stringList.add("fe");
    stringList.add("e");
    stringList.add("ABC");
    stringList.add("Abc");
    stringList.add("ABc");

    System.out.print("Before Sort:");
    System.out.println(stringList);

    Collections.sort(stringList, new Comparator<String>(){
        @Override
        public int compare(String o1, String o2) {
                if(o1.length() > o2.length())
                {
                    return 1;
                }
                else if(o1.length() < o2.length()){
                    return -1;
                }
                else if(o1.length() == o2.length()){
                    return return o1.compareTo(o2);
                }
                else return 0;
        }
    });
    System.out.print("After Sort :");
    System.out.println(stringList);
}

以上代码根据长度对列表进行排序,但未能根据区分大小写进行排序。

它可以输出 [a,b,e,fe,ABC,ABc,Abc,abc]

预期输出 排序后:[a,b,e,fe,abc,Abc,ABc,ABC]

任何帮助。

3 个答案:

答案 0 :(得分:2)

Option Explicit

Sub Get_OA_Data()

Dim wkb2 As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Dim rng As Range, xCell As Range
Dim LR As Long, LC As Long, LR2 As Long

Set ws = ThisWorkbook.Worksheets("Data Entry")
Set wkb2 = Workbooks.Open("\\srvabdotfpr08\PC_APPS\forum\Gateshead Serialisation\sys_serialisation1.csv")
Set ws2 = wkb2.Worksheets("sys_serialisation1")

ws.Range("I6:I7").ClearContents
LR2 = ws2.Range("A" & ws.Rows.Count).End(xlUp).Row

For Each xCell In ws2.Range("A1:A" & LR2)
    xCell.EntireRow.Hidden = Left(xCell.Value, 6) <> ws.Range("F6")
Next xCell

LR = ws2.Range("A" & ws.Rows.Count).End(xlUp).Row
LC = ws2.Cells(1, ws.Columns.Count).End(xlToLeft).Column

Set rng = ws2.Range(ws2.Cells(1, 1), ws2.Cells(LR, LC))
    rng.SpecialCells(xlCellTypeVisible).Copy
    ws2.Range("D12").PasteSpecial xlPasteValues

End Sub

与您的规格相矛盾。如果要区分大小写,则不应该将它与忽略大小写进行比较,而将其转换为小写。

答案 1 :(得分:2)

我认为您需要考虑更多的情况,例如AFefE。您的排序似乎没有自然的流程。下面的代码获取所需的输出。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author blj0011
 */
public class JavaApplication114
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        List<String> stringList = new ArrayList<>();
        stringList.add("a");
        stringList.add("abc");
        stringList.add("b");
        stringList.add("fe");
        stringList.add("e");
        stringList.add("ABC");
        stringList.add("Abc");
        stringList.add("ABc");

        System.out.print("Before Sort:");
        System.out.println(stringList);

        Collections.sort(stringList, new Comparator<String>()
        {
            @Override
            public int compare(String o1, String o2)
            {
                if (o1.length() > o2.length()) {
                    return 1;
                }
                else if (o1.length() < o2.length()) {
                    return -1;
                }
                else if (o1.length() == o2.length()) {
                    if (o1.length() == 1) {
                        return o1.compareTo(o2);
                    }
                    else {
                        return o2.compareTo(o1);
                    }

                }
                else {
                    return 0;
                }
            }
        });
        System.out.print("After Sort :");
        System.out.println(stringList);
    }
}
  

输出:

run:
Before Sort:[a, abc, b, fe, e, ABC, Abc, ABc]
After Sort :[a, b, e, fe, abc, Abc, ABc, ABC]
BUILD SUCCESSFUL (total time: 0 seconds)

答案 2 :(得分:2)

'a'>'A'以自然顺序。如果要按降序对字符串进行排序,请将o1.compareTo(o2);更改为o2.compareTo(o1);作为旁注,可以使用Integer.compare(int x, int y)来消除重复的内容(如果没有的话)。示例:

    Collections.sort(stringList, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            int comp = Integer.compare(o1.length(), o2.length());
            if (comp == 0) {
                return o1.length()==1 ? o1.compareTo(o2):o2.compareTo(o1);
            } 
             return comp;
        }
    });