All Posts All Posts

Lexicographic Sorting Method for Strings with Mixed Numbers, Uppercase and Lowercase Letters

November 4, 2016·
Software Engineering
·3 min read
Tecker Yu
Tecker Yu
AI Native Cloud Engineer × Part-time Investor

Core Steps Overview

Filtering

Filter out numbers and other special symbols, keeping only letters

The replaceAll method built into the String class and regular expressions will be used for filtering

Splitting

Split the string into single characters

The substring method built into the String class will be used here

Sorting

Sort the single-character String array obtained from splitting in a case-insensitive manner

The sort method in the Arrays class will be used here

Key Code Demonstration

Preface

Since the number of input lines and string content are arbitrary, each line’s string is directly added to Java’s ArrayList dynamic array, then transferred to a String array newArray initialized with length, thus avoiding the risk of forced type conversion from Object type to String[].


String Filtering Demonstration

for (int i=0;i <elementIndex;i++)
newArray[i] = newArray[i].replaceAll("\\d+","");
//This demonstrates removing numbers

Extension:

.replaceAll("escape character + regular expression", "replacement character");

String Splitting Demonstration

for(int k=0;k<newArray[number].length();k++)
sortArray[k] = newArray[number].substring(k,k+1);
//Use a for loop to cut one character at a time and add it to sortArray

Extension:

Common String Class Methods

Method PurposeDefinition
Extract substring from start to end indexsubstring(int beginIndex,int endIndex)
Use a character as delimitersplit(String regex) return String[]
Convert to uppercase letterstoUpperCase()
Convert to lowercase letterstoLowerCase()
Remove leading and trailing spacestrim() return a string
Compare stringsboolean equals(Object anObject)

Single Character String Array Sorting Demonstration

Arrays.sort(sortArray,String.CASE_INSENSITIVE_ORDER);
//Sort case-insensitively

Extension:

Common Arrays Class Methods

Method PurposeDefinition
Sort arraysort()
Find value in arraybinarySearch(object[ ], object key) *First use sort() then search
Copy arrayint[] copyOf(int[] original,int newLength)
Replace values in arrayfill(int[] a,int val)
To be continued….

Complete Code Demo

/**
 * Created by Tecker on 2016/6/21.
 */
import java.util.*;

public class UsingArrayList {
    public static void main(String[] args){
        int elementIndex = 1;
        Scanner inputElement = new Scanner(System.in);
        ArrayList elementlist = new ArrayList();
        boolean inputOver = false;
        System.out.printf("Please enter N lines of N strings\n");
        System.out.printf("End when inputOver is entered\n");

        do {
            System.out.printf("Enter the " + elementIndex + "th string\n");
            System.out.printf(">>");
            String input = inputElement.nextLine();
            if (input.equals("inputOver")){
                inputOver = true;
                System.out.println("Input completed!");
            }
            else {
                elementlist.add(input);
                elementIndex++;
            }
        }while (inputOver == false);      //Use do-while statement with if judgment to control input of N lines of mixed case strings with numbers
        elementIndex = elementIndex - 1;
        System.out.printf("Before sorting:\n");
        String[] newArray = new String[elementIndex];
        for (int k=0;k <elementIndex;k++)
            System.out.printf(">> " + elementlist.get(k) + "\n");
        for (int index=0;index <elementIndex;index++)
            newArray[index] = elementlist.get(index).toString();
        for (int i=0;i <elementIndex;i++)
            newArray[i] = newArray[i].replaceAll("\\d+","");
        for (String element : newArray)
            System.out.printf(element +"\n");
        System.out.println("After sorting\n");
        for (int listIndex=0;listIndex <newArray.length;listIndex++){
            sortArrays(listIndex,newArray);
        }


    }
    public static void sortArrays(int number,String[] newArray){
        String[] sortArray = new String[newArray[number].length()];
        for(int k=0;k<newArray[number].length();k++){
            sortArray[k] = newArray[number].substring(k,k+1);
        }
        Arrays.sort(sortArray,String.CASE_INSENSITIVE_ORDER);
        String finalString = ">>";
        for (int j=0;j<sortArray.length;j++){
            finalString = finalString.concat(sortArray[j]);
        }
        System.out.println(finalString);
    }

}

Views