字符串的更改排序顺序包括特殊字符(例如"

编程入门 行业动态 更新时间:2024-10-12 05:47:27
本文介绍了字符串的更改排序顺序包括特殊字符(例如"_")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

PHP脚本以降序输出电子邮件地址列表,如下所示:

A PHP script outputs a list of e-mail addresses in descending order like following:

_abc_@testmail _abc45_@testmail _abc2_@testmail ypaux2aux@yahoo yaremchuk56@testmail vasillevn@hotmail ugur@hotmail twes@gmail tukaux@yahoo ttsetaux1@yahoo tra@testmail

在Java中,我从这些电子邮件中创建一个ArrayList,然后以降序排序.结果是不同的:

In Java, I'm creating an ArrayList from these e-mails, then sorting in descending order. The result is different:

ypaux2aux@yahoo yaremchuk56@testmail vasillevn@hotmail ugur@hotmail twes@gmail tukaux@yahoo ttsetaux1@yahoo tra@testmail _abc45_@testmail _abc2_@testmail _abc_@testmail

差异是由下划线"_"引起的.我想实现与PHP脚本相同的排序顺序.我怎样才能做到这一点?我无法访问PHP代码.

The difference is caused by the underscore "_". I want to achieve the same sort order as the PHP script. How can I do this? I've no access to the PHP code.

我使用的Java测试代码是:

The Java test code I used is:

import java.util.ArrayList; import java.util.Collections; public class sorty { public static void main(String[] args) { ArrayList<String> listStrings = new ArrayList<>(); listStrings.add("_abc_@testmail"); listStrings.add("_abc45_@testmail"); listStrings.add("_abc2_@testmail"); listStrings.add("ypaux2aux@yahoo"); listStrings.add("yaremchuk56@testmail"); listStrings.add("vasillevn@hotmail"); listStrings.add("ugur@hotmail"); listStrings.add("twes@gmail"); listStrings.add("tukaux@yahoo"); listStrings.add("ttsetaux1@yahoo"); listStrings.add("tra@testmail"); for (int i = 0; i < listStrings.size(); i++) { System.out.println(listStrings.get(i)); } Collections.sort(listStrings); Collections.reverse(listStrings); for (int i = 0; i < listStrings.size(); i++) { System.out.println(listStrings.get(i)); } ; } }

推荐答案

我会使用适当的 Collat​​or .实现自己的比较器不是最琐碎的事情.最好的是,如果您对默认值之一满意.例如

I would use an appropriate Collator. Implementing your own comparator isn't the most trivial thing. The nicest would be if you where happy with one of the defaults. e.g.

Collections.sort(listStrings, Collator.getInstance(Locale.US));

或类似.

如果没有一个适合您,则使用基于规则的整理程序将使您的意图清除器,然后实施一个比较器imo:

If none of the existing ones works for you then using a rule based collator would make your intent clearer then implementing a comparator imo:

String rules = "< a < b < c < '_'" //etc Collections.sort(listStrings, new RuleBasedCollator(rules));

更多推荐

字符串的更改排序顺序包括特殊字符(例如"

本文发布于:2023-11-10 14:20:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   顺序   特殊字符   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!