将字符串转换为数字以进行比较
我需要你帮助解决一个小问题。
是否有可能(在 Java 或抽象解决方案中)将任意字符串转换为具有以下属性的整数:字母顺序也适用于整数?
例子: 房子<树<动物园按字母顺序排列。
我想将这些字符串转换为整数,以便排序也可用。重要的是,具体的字符串之前是未知的。这意味着它应该是一个独特的转变。
我希望有人能帮助我。
问候, 迈克尔
I need your help with a small problem.
It there a possibility (in Java or an abstract solution) to transform a arbitrary String into an Integer with the property, that the alphabetically ordering does also work with the Integer?
Example:
House < Tree < Zoo in alphabetically order.
I would like to transform those Strings into Integer, so that the ordering is also available. Important is, that the concrete Strings are not known before. That means it should be a unique transformation.
I hope someone can help me.
Regards,
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。假设您有两个代表 1 和 2 的字符串。您始终可以创建另一个按字母顺序排列在它们之间的字符串,例如,
如果您知道正在处理的字符串有最大长度,那么这是可能的。例如,假设您的字符串长度 <= 3。取一个字符串 abc,并将每个字母转换为数字,其中 A = 1,B = 2,...,Z = 26。如果字符串小于 3字符长,末尾的空白处填零。则其值为:
则:
No, it's not possible. Let's say you have two strings which represent 1 and 2. You can always make another string which would fit between them alphabetically e.g.
If you know the strings you're dealing with have a maximum length, then this is possible. For example, suppose your string has length <= 3. Take a string abc, and convert each letter to a number, where A = 1, B = 2, ..., Z = 26. If the string is less than 3 characters long, fill in the blanks at the end with zeroes. Then the value is:
Then: