java algorithmic programming problem: String catalog sorting

Title: sort the list of strings. Take the list of some books as an example, as shown in the following figure. For example, the first chapter is 1.1, the first chapter is 1.2, the first chapter is 1.1.1, the second chapter is 2 The first section of the second chapter of the fourth chapter is 4.2.1. If a string of directory arrays (custom) is disordered, please arrange them in ascending order.

In fact, the idea of this problem is very simple. The key is to determine the size of two strings. After finding out who is bigger and who is smaller, you can sort the array of string numbers with a bubble sort or a selective sort.
Method 1: I wrote it.

import java.time.LocalDateTime;

import java.util.Arrays;

/**
 * String catalog sort
 * 
 * @author zql
 */
public class Catalog {
	
	public static void main(String[] args) {
		Catalog c = new Catalog();
		String[] str = new String[] { "2", "1.1.2", "2.1", "1", "2.1.4", "1.1", "3.2", "3", "2.1.3", "1.1.1", "1.1.1.1", "2.3" };
		System.out.println("Directory before sorting:" + Arrays.toString(str));
		str = c.sortStr(str);
		System.out.println("Sorted Directory:" + Arrays.toString(str));
	}
	
	/**
	 * Sort arrays by selecting sort
	 * 
	 * @param str Array to sort
	 * @return Sorted array
	 */
	public String [] sortStr(String [] str) {
		String temp = null;
		for (int i = 0; i < str.length - 1; i++) {
			for (int j = i + 1; j < str.length; j++) {
				if (compareSize(str[i], str[j])) {
					temp = str[i];
					str[i] = str[j];
					str[j] = temp;
				}
			}
		}
		return str;
	}
	
	/**
	 * This method compares the size of str1 and str2
	 * 
	 * @param str1 First string to compare
	 * @param str2 Second string to compare
	 * @return str1 If it is greater than str2, it will return true; otherwise, it will return false.
	 */
	private boolean compareSize(String str1, String str2) {
		// Pay attention to using translational characters\
		String[] s1 = str1.split("\\.");
		String[] s2 = str2.split("\\.");
		// Find the shortest size of array length
		int min = s1.length > s2.length ? s2.length : s1.length;
		int[] a1 = new int[min];
		int[] a2 = new int[min];
		// Convert each character of a separate string array to int
		for (int i = 0; i < min; i++) {
			a1[i] = Integer.parseInt(s1[i]);
			a2[i] = Integer.parseInt(s2[i]);
			if (a1[i] > a2[i]) {
				return true;
			} else if (a1[i] < a2[i]) {
				return false;
			}
		}
		// When the previous parts are the same, the length of the string is judged. A large length indicates that the directory sequence is large
		if (s1.length > s2.length) {
			return true;
		} else {
			return false;
		}
	}
}

Method 2: my Keng than the old students wrote, a simple topic is simply written out the ritual sense of the project, not yet annotated, I am dizzy.
Test class:

import java.util.Iterator;
import java.util.TreeSet;

/**
 * Created by HU on 2018/5/28.
 */
public class MyTest {
    static String SourceStr[]={"1","1.2","4","8","2.2","5","5.2","5.2.1","5.2.1.1.1",
            "4.2","4.1","2","3","4.2","4.3","4.4","2.1","2.1.2","1.1"};
    static  StringBuilder sTemp=new StringBuilder();
    public static void main(String[] args) {

        TreeSet<Node> ts = new TreeSet<>();
        for (int i = 0,n=SourceStr.length; i <n ; i++) {
            ts.add(new Node(SourceStr[i], SourceStr[i].split("\\.").length));
        }

        Iterator<Node> it = ts.iterator();
        while(it.hasNext()) {
            System.out.println(it.next().getName());
        }
    }
}

Implementation class:

/**
 * Created by HU on 2018/5/28.
 */
public class Node implements Comparable<Node>{
    String name;
    int length;

    public Node() {}

    public Node(String name, int length) {
        this.name = name;
        this.length = length;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
	
    public int compareTo(Node o) {
        if(this.name.equals(o.name))return 0;
        if(this.length>o.length){
            if(this.name.startsWith(o.name)){
              return 1;
            }
        }else if(this.length<o.length){
            if(o.name.startsWith(this.name)){
                return -1;
            }
        }
        int index=0;
        while(Integer.valueOf(this.name.charAt(index))==Integer.valueOf(o.name.charAt(index))){
            index++;
        }
        return Integer.valueOf(this.name.charAt(index))-Integer.valueOf(o.name.charAt(index));
    }
}

55 original articles published, 25 praised, 10000 visitors+
Private letter follow

Tags: Java

Posted on Sun, 12 Jan 2020 12:01:36 -0500 by Darhazer