java algorithm programming problem: string directory sort

Title: string sort directory to directory certain books for example, as shown below, a first article such as 1, a first first chapter 1.1, the first to the second chapter 1.2, the first to the second Section 1.1.1 is a chapter, the second is 2, ..., Part IV Chapter II Section 4.2.1 is, if there are a bunch of catalog arrays (custom) is a mess, please send it by ascending lined up.
Here Insert Picture Description

Thinking this question is very simple, the key is to determine the size of two strings, who is great who get little later, with a bubble sort or selection sort can be sorted number array of strings.
Method One: I wrote.

import java.time.LocalDateTime;

import java.util.Arrays;

/**
 * 字符串目录排序
 * 
 * @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("排序前的目录:" + Arrays.toString(str));
		str = c.sortStr(str);
		System.out.println("排序后的目录:" + Arrays.toString(str));
	}
	
	/**
	 * 通过选择排序对数组进行排序
	 * 
	 * @param str 要进行排序的数组
	 * @return 排序后的数组
	 */
	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;
	}
	
	/**
	 * 本方法要比较的是str1与str2的大小
	 * 
	 * @param str1 要比较的第一个字符串
	 * @param str2 要比较的第二个字符串
	 * @return str1大于str2则返回true;反之,则返回false。
	 */
	private boolean compareSize(String str1, String str2) {
		// 注意点要用转译字符\\
		String[] s1 = str1.split("\\.");
		String[] s2 = str2.split("\\.");
		// 求出数组长度最短的大小
		int min = s1.length > s2.length ? s2.length : s1.length;
		int[] a1 = new int[min];
		int[] a2 = new int[min];
		// 把分开的字符串数组的每一个字符转成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;
			}
		}
		// 前面部分都一样的时候,则判断字符串的长度,长度大说明该目录序列是大的
		if (s1.length > s2.length) {
			return true;
		} else {
			return false;
		}
	}
}

Method Two: I write the pit than the old school, a topic he just wrote a simple sense of ritual items, no comment, I fainted.
Test categories:

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());
        }
    }
}

Implementing Classes:

/**
 * 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));
    }
}

Published 55 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/mr_zql/article/details/100086892