我怎样才能按字母顺序获得这个插入排序代码?

How could I get this Insertion Sort code in alphabetical order?

我是编码新手,我将我的代码按字母从最少到最多的顺序排列。请帮助我了解如何将代码按字母顺序排列。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] a = new String[] {"bread", "milk", "cheese", "spinach", "apple", "peanuts"};
        System.out.println("Before Sort:" + Arrays.toString(a));
        insertionSort(a);
        System.out.println("After Sort: " + Arrays.toString(a));

        System.out.println();

        a = new String[] { "Allison", "Neha", "Charley", "Jason", "Tyson", "Miles", "Riley" };
        System.out.println("Before Sort:" + Arrays.toString(a));
        insertionSort(a);
        System.out.println("After Sort: " + Arrays.toString(a));


    }

    public static void insertionSort(String[] a) {
        // Move the marker from index 1 to the last index
        for (int i = 1; i < a.length; i++) {
            // Insert the element at the marker to the right position
            insert(a, i);

        }
    }
    public static void insert(String[] a, int marker) {
        String unsortedElement = a[marker];

        // shift other elements to the right to create the correct position
        int correctPosition = marker;
        for (int i = marker - 1; i >= 0; i--) {
            if (a[i].length() > unsortedElement.length()) {
                a[i + 1] = a[i];
                correctPosition--;
            }
            else {
                break; // stop looping
            }
        }
        // Insert the unsorted element to the correct position
        a[correctPosition] = unsortedElement;
    }
}

您可以使用静态方法一次对孔数组进行排序Arrays.sort(arr)

import java.util.Arrays;
...

String[] arrayOfStrs = {"d","a","ca"};

Arrays.sort(arrayOfStrs);

// arrayOfStrs is now ["a","ca","d"]

要将字母从少到多排序,首先创建一个使用 String 长度的 Comparator class:

public class StringLengthComparator implements Comparator<String> {
  public int compare(String o1, String o2) {
    return Integer.compare(o1.length(), o2.length());
  }
}

然后在Arrays.sort(...)方法中使用:

Arrays.sort(a, new StringLengthComparator());

要按字母顺序对它们进行排序,您只需要:

Arrays.sort(a);

您必须更改 insert() 方法中的条件。

可以使用方法 compareTo.

比较字符串对象

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String)

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

Returns: the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

换句话说,您可以将 compareTo() 方法的作用想象成 'weighting' 使用标度的那些字符串。

如果 'weights' 等于 compareTo() returns 0,如果第一个字符串 'lighter' 比第二个(字符串作为参数传递)a 'scales' (compareTo()) 会给你一个负值。

所以下面代码中的条件写成

unsortedElement.compareTo(a[i]) < 0

    public static void insert(String[] a, int marker) {
        String unsortedElement = a[marker];

        // shift other elements to the right to create the correct position
        int correctPosition = marker;
        for (int i = marker - 1; i >= 0; i--) {
            if (unsortedElement.compareTo(a[i]) < 0) {
                a[i + 1] = a[i];
                correctPosition--;
            }
            else {
                break; // stop looping
            }
        }
        // Insert the unsorted element to the correct position
        a[correctPosition] = unsortedElement;
    }

输出:

Before Sort:[Allison, Neha, Charley, Jason, Tyson, Miles, Riley]
After Sort: [Allison, Charley, Jason, Miles, Neha, Riley, Tyson]