将一句话去除符号后按大小排序

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wdz985721191/article/details/88235914
public static void main(String[] args) {
    String str = "Where are you from? I have an apple,but i want to home";
    // 大小写转换
    str = str.toLowerCase();
    String s = reveresStr(str);
    String s1 = sortStr(str);
    System.out.println(s);
    System.out.println(s1);
}

/***
 * @Description: 字符串翻转 
 * @param str
 * @return java.lang.String 
 * @throws
 * @author wdz
 * @date 2019/3/6 14:50 
 */
public static String reveresStr(String str){
    StringBuilder sb = new StringBuilder();
    // 根据非字母分割,剩余的全部是字母
    String[] split = str.split("[^a-zA-Z]");
    for (int i = 0; i < split.length; i++) {
        String s = split[i].toLowerCase();
        sb.append(s);
    }
    sb.reverse();
    return sb.toString();
}

/***
 * @Description: 按大小排序 冒泡排序
 * @param str
 * @return java.lang.String 
 * @throws
 * @author wdz
 * @date 2019/3/6 14:51 
 */
public static String sortStr(String str){
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length-1; i++) {
        for (int j = 0; j < chars.length-i-1; j++) {
            // < > 控制大小排序
            if(chars[j] < chars[j+1]){
                char c = chars[j + 1];
                chars[j + 1] = chars[j];
                chars[j] = c;
            }
        }
    }
    StringBuilder s = new StringBuilder();
    for (char c :chars) {
        if(Character.isLetter(c)){
            s.append(c);
        }
    }
    return s.toString();
}
/***
 * @Description: 选择排序
 * @param str
 * @return java.lang.String 
 * @throws
 * @author wdz
 * @date 2019/3/6 15:09 
 */
private static String selectSort(String str) {
    char[] chars = str.toCharArray();
    int len = chars.length;
    char temp;
    int min;
    for (int i = 0; i < len-1; i++) {
        min = i;
        for (int j = i+1; j < len; j++) {
            // 控制大小排序
            if(chars[j] < chars[min]){
                min = j;
            }
        }
        temp = chars[i];
        chars[i] = chars[min];
        chars[min] = temp;
    }
    StringBuilder st = new StringBuilder();
    for (int i = 0; i < len; i++) {
        if(Character.isLetter(chars[i])){
            st.append(chars[i]);
        }
    }
    return st.toString();
}
/***
 * @Description: 插入排序
 * @param str
 * @return java.lang.String
 * @throws
 * @author wdz
 * @date 2019/3/6 15:21
 */
public static String insertSort(String str){
    char[] chars = str.toCharArray();
    char cur;
    int pre;
    int len = chars.length;
    for (int i = 1; i < len; i++) {
        cur = chars[i];
        pre = i - 1;
        while(pre >= 0 && chars[pre] < cur){
            chars[pre + 1] = chars[pre];
            pre--;
        }
        chars[pre + 1] = cur;
    }
    StringBuilder st = new StringBuilder();
    for (int i = 0; i < len; i++) {
        if(Character.isLetter(chars[i])){
            st.append(chars[i]);
        }
    }
    return st.toString();
}

 

/***
 * @Description: 使用Collections.sort排序
 * @param str
 * @return java.lang.String
 * @throws
 * @author wdz
 * @date 2019/3/6 15:50
 */
public static String collectionSort(String str){
    List<String> list = new ArrayList<>(str.length());
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if(Character.isLetter(chars[i])){
            char s = chars[i];
            list.add(String.valueOf(s));
        }
    }
    // 从小到大排序
    // Collections.sort(list);
    Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            // 参数调换位置可改变排序规则
            return o2.compareTo(o1);
        }
    });
    StringBuilder sb = new StringBuilder(list.size());
    for (String s : list) {
        sb.append(s);
    }
    return sb.toString();
}

结果
emohottnawitubelppanaevahimorfuoyeraerehw
ywwvuutttrrrppoooonnmmliihhhfeeeeeebaaaaa

更多请查看https://www.cnblogs.com/onepixel/articles/7674659.html,本文参考上述连接对字符串进行的排序,以上纯属个人理解,

展开阅读全文

Linq: 想按数字大小排序,而非按字符串"数字"大小排序。应该怎么实现?

08-27

[code=C#]rn List dots = new List();rn dots.Add(new Dot dt=DateTime.Now, Father="1号",Name="1号点",Value=12.121f);rn dots.Add(new Dot dt = new DateTime(2010,2,3,21,21,12), Father = "2号", Name = "2号点", Value = 12.121f );rn dots.Add(new Dot dt = new DateTime(2010, 2, 3, 1, 1, 4), Father = "3号", Name = "3号点", Value = 12.121f );rn dots.Add(new Dot dt = new DateTime(2010, 3, 3, 23, 2, 3), Father = "10号", Name = "10号点", Value = 12.121f );rn dots.Add(new Dot dt = new DateTime(2010, 4, 1, 21, 21, 6), Father = "11号", Name = "11号点", Value = 12.121f );rn dots.Add(new Dot dt = new DateTime(2010, 1, 2, 8, 22, 8), Father = "12号", Name = "12号点", Value = 12.121f );rn dots.Add(new Dot dt = new DateTime(2010, 2, 12, 9, 3, 12), Father = "20号", Name = "20号点", Value = 12.121f);rn dots.Add(new Dot dt = new DateTime(2010, 7, 13, 11, 6, 11), Father = "21号", Name = "21号点", Value = 12.121f );rn dots.Add(new Dot dt = DateTime.Now, Father = "21号", Name = "21号点", Value = 12.121f );rn dots.Add(new Dot dt = DateTime.Now, Father = "2号", Name = "2号点", Value = 12.121f);rn dots.Add(new Dot dt = DateTime.Now, Father = "10号", Name = "10号点", Value = 12.121f );rn dots.Add(new Dot dt = DateTime.Now, Father = "11号", Name = "11号点", Value = 12.121f );rn //时间分组rn var ds = from d in dots group d by d.dt.ToString();rn DataTable table = new DataTable();rn table.Columns.Add("1");rn table.Columns.Add("2");rn table.Columns.Add("3");rn table.Columns.Add("4");rn foreach (var g in ds)rn rn //因为每个Father名称都以数字开始命名,故(ˇˍˇ) 想按数字大小排序,而非按字符串"数字"大小排序。应该怎么实现?rn var ds2 = from d2 in g orderby d2.Father.Substring(0,2) orderby d2.Name select d2;rnrn foreach (var d in ds2)rn rn table.Rows.Add(new object[] d.dt,d.Father,d.Name,d.Value );rn rn rn dataGridView1.DataSource = table.DefaultView;rnrnrn[/code] 论坛

没有更多推荐了,返回首页