【问题标题】:to sort the word based on the number characters that contains in java [duplicate]根据java中包含的数字字符对单词进行排序[重复]
【发布时间】:2013-08-27 23:12:48
【问题描述】:

如何根据java中包含的数字字符对单词进行排序? 例如

String Given : "my name is dhana"
O/p should be : "dhana name my is"

【问题讨论】:

  • 你试过谷歌搜索“按长度排序java字符串”这个主题吗?
  • 到目前为止你尝试了什么?
  • 好的,继续。如果你卡在某个地方,请回来。

标签: java string sorting


【解决方案1】:
  • string

  • splitit by space(成文字)

  • array转换成ArrayList

  • 以您想要的方式创建自定义 comparatorsort(此处为 length

【讨论】:

  • 在这里投反对票?有趣...
  • 不用转成列表,数组也可以轻松排序...
【解决方案2】:

使用这个

  public void func()
        {
        String input =  "my name is dhana";
        String input_array[] = input.split(" ");
        Collections.sort(input_array, new CustomComparator());
        print_Array(input_array);
       }

CustomComaparator.java

public class CustomComparator implements Comparator<String>
   {
      public int compare(String a, String b) {  
      if (a.length() > b.length()) {
         return -1;
      } else if (a.length() < b.length()) {
         return 1;
      }
      return a.compareTo(b);
    }
}

【讨论】:

  • 像这样编码的CustomComparator 将按升序而不是降序对列表进行排序。
  • 感谢您通知我 :) 我更改了它。
【解决方案3】:

您可以使用Comparator,它首先按长度进行比较,如果长度相同,则使用String.compareTo()

【讨论】:

    【解决方案4】:

    这是一种不需要创建自定义Comparator 的替代方法。我只是为了完整性而提出它。

    1. 将字符串拆分为单词。
    2. 创建一个 SortedMap。
    3. 迭代单词列表。
    4. 用 "%03d%05d".format(999-aWord.length(),i) -> aWord 填充它,其中 i 是 aWord 在单词列表中的索引。这里,key 的形式是 xxxyyyyy,其中 xxx 是字长的倒数(998 表示 l=1,997 表示 l=2,等等),所以排序如果从最长到最短,并且 yyyyy 允许区分相同长度的单词(以及相同单词的多次出现)。
    5. 结果是 theMap.values()。
    String input= "This is a string with differently sized words. This is another sentence." ;
    String[] splitInput= input.split("[ .]") ;
    TreeMap<String,String> theMap= new TreeMap<String,String>() ;
    int index= 0 ;
    for(String word: splitInput ) {
        if( word.length() > 0 ) {
            String key= String.format("%03d%05d",(999-word.length()),index) ;
            theMap.put(key,word);
            index++;
        }
    }
    System.out.println(theMap.values());
    

    产生输出:

    [differently, sentence, another, string, sized, words, This, with, This, is, is, a]
    

    ,这是正确的。其实Strings大小相同,在input中是按位置列出的。

    【讨论】:

      【解决方案5】:

      在拆分时使用正则表达式可以解决您的问题:

          String str = "my name is dhana";
          List<String> items = Arrays.asList(str.split("\\s+"));
      
          print(items);
      
          Collections.sort(items, new Comparator<String>() {
      
                  @Override
                  public int compare(String s0, String s1) {
                                  // Descending order
                      if (s0.length() < s1.length())
                          return 1;
                      else if (s0.length() > s1.length())
                          return -1;
                      return 0;
                  }
      
              });
      
      
          String descOrderedString = "";
          for (String item : items) {
              descOrderedString += item + " ";
          }
      
          System.out.println(descOrderedString);
      

      print() 方法可以是这样的:

      public void print(List<String> list) {
          for(String s: list){
              System.out.println(s);
          } 
      }
      

      输出:

      对于print(items) 是:

      my
      name
      is
      dhana
      

      对于System.out.println(descOrderedString) 是:

      dhana name my is 
      

      【讨论】:

      • 错误:线程“main”中的异常 java.lang.NullPointerException
      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      相关资源
      最近更新 更多