search

Home  >  Q&A  >  body text

java - solr sort by pinyin alphabetical order

There is a title field, and I want to sort it by the first letter of Pinyin. There is a method in the solr method

params.setSort("title",ORDER.asc);//排序字段,正序还是倒序

But it seems that the result of this setting is not what I want. The result I want is as follows:
The first letter of the serial number title
1 aa
2 countryg
3 person r
4中z

In the SQL statement, I can sort by the first letter of Pinyin. I want to know if I can sort by the first letter of Pinyin in Solr? What should I do? Can I ask for guidance? ? ?

黄舟黄舟2750 days ago843

reply all(2)I'll reply

  • 黄舟

    黄舟2017-05-17 10:04:57

    Can’t you just create a new field with the “first letter of title” as the value as a field? Generally speaking, in practical applications, custom fields are used as sorting conditions

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-17 10:04:57

    There is a relatively simple method, as follows:

    List<String> strs = new ArrayList<>();
    strs.add("中");
    strs.add("啊");
    strs.add("坎");
    strs.add("哈");
    strs.add("少");
    strs.add("发");
    strs.add("顶");
    strs.add("擦");
    strs.add("鹅");
    strs.add("巴");
    
    // 排序
    strs.sort(Collator.getInstance(Locale.CHINA));
    // 打印
    strs.forEach(System.out::println);
    

    Result

    啊 // A
    巴 // B
    擦 // C
    顶 // D
    鹅 // E
    发 // F
    哈 // H
    坎 // K
    少 // S
    中 // Z

    You can also use pinyin4j for more complex sorting

    reply
    0
  • Cancelreply