Java 字符+和的奇怪排序/
我们有一位客户要求我们以这种方式对一些字符串进行排序,以便与旧工具兼容:Java 字符+和的奇怪排序/,java,.net,Java,.net,我们有一位客户要求我们以这种方式对一些字符串进行排序,以便与旧工具兼容: A IR / A IR +100bp B IR / B IR +100bp 正如您可以看到的,“/”和“+”是颠倒的,标准顺序是相反的 A IR +100bp A IR / B IR +100bp B IR / 这两个字符都是低ASCII表的一部分,非常稳定 有人知道第一个顺序来自哪种语言或算法吗?我想这不是最后一个惊喜吧?简短的回答是:因为它使用并返回相同的结果: 较长
A IR /
A IR +100bp
B IR /
B IR +100bp
正如您可以看到的,“/”和“+”是颠倒的,标准顺序是相反的
A IR +100bp
A IR /
B IR +100bp
B IR /
这两个字符都是低ASCII表的一部分,非常稳定
有人知道第一个顺序来自哪种语言或算法吗?我想这不是最后一个惊喜吧?简短的回答是:因为它使用并返回相同的结果: 较长的答案是:它不是ASCII/Unicode码点序数值比较,而是中定义的Unicode字符串比较 后一个文件指向中提供的。此表为所有显式加权字符提供了从字符到排序规则元素的映射: 以下有趣的示例是在基于.NET的Powershell中编写的,因为我不会说Java: 结果: 为完整起见,Get CharInfo函数定义如下:
<#
_get-CharInfo_1.1.ps1
Origin by: http://poshcode.org/5234
sorry, the above link is not available; last time checked 2018-05-07
Improved by: https://stackoverflow.com/users/3439404/josefz
Use this like this: "ábč",([char]'x'),0xBF | Get-CharInfo
Activate dot-sourced like this (apply a real path instead of .\):
. .\_get-CharInfo_1.1.ps1
#>
Add-Type -Name UName -Namespace Microsofts.CharMap -MemberDefinition $(
switch ("$([System.Environment]::SystemDirectory -replace
'\\', '\\')\\getuname.dll") {
{Test-Path -LiteralPath $_ -PathType Leaf} {@"
[DllImport("${_}", ExactSpelling=true, SetLastError=true)]
private static extern int GetUName(ushort wCharCode,
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder buf);
public static string Get(char ch) {
var sb = new System.Text.StringBuilder(300);
UName.GetUName(ch, sb);
return sb.ToString();
}
"@
}
default {'public static string Get(char ch) { return "???"; }'}
})
function Get-CharInfo {
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject],[System.Array])]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
$InputObject
)
begin {
Set-StrictMode -Version latest
function out {
param(
[Parameter(Position=0, Mandatory=$true )] $ch,
[Parameter(Position=1, Mandatory=$false)]$nil=''
)
if (0 -le $ch -and 0xFFFF -ge $ch) {
[pscustomobject]@{
Char = [char]$ch
CodePoint = 'U+{0:X4}' -f $ch
Category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($ch)
Description = [Microsofts.CharMap.UName]::Get($ch)
}
} elseif (0 -le $ch -and 0x10FFFF -ge $ch) {
$s = [char]::ConvertFromUtf32($ch)
[pscustomobject]@{
Char = $s
CodePoint = 'U+{0:X}' -f $ch
Category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($s, 0)
Description = '???' + $nil
}
} else {
Write-Warning ('Character U+{0:X} is out of range' -f $ch)
}
}
}
process {
if ($PSBoundParameters['Verbose']) {
Write-Warning "InputObject type = $($InputObject.GetType().Name)"}
if ($null -cne ($InputObject -as [char])) {
#Write-Verbose "A $([char]$InputObject) InputObject character"
out $([int][char]$InputObject) ''
} elseif ($InputObject -isnot [string] -and $null -cne ($InputObject -as [int])) {
#Write-Verbose "B $InputObject InputObject"
out $([int]$InputObject) ''
} else {
$InputObject = [string]$InputObject
#Write-Verbose "C $InputObject InputObject.Length $($InputObject.Length)"
for ($i = 0; $i -lt $InputObject.Length; ++$i) {
if ( [char]::IsHighSurrogate($InputObject[$i]) -and
(1+$i) -lt $InputObject.Length -and
[char]::IsLowSurrogate($InputObject[$i+1])) {
$aux = ' 0x{0:x4},0x{1:x4}' -f [int]$InputObject[$i],
[int]$InputObject[$i+1]
Write-Verbose "surrogate pair $aux at position $i"
out $([char]::ConvertToUtf32($InputObject[$i], $InputObject[1+$i])) $aux
$i++
} else {
out $([int][char]$InputObject[$i]) ''
}
}
}
}
}
这是一个如何与.net/Java相关的问题?我们正在用Java开发,旧的工具是.net。这是一个算法…也许第一个顺序来自.net…降序?两者都一样…非常感谢,这就是它。他们使用.NET不变量文化(Java中的英语)进行排序
002B ; [*063F.0020.0002] # PLUS SIGN
002F ; [*03A1.0020.0002] # SOLIDUS
( '+-/*+−÷×' |
Get-CharInfo |
Format-Table -HideTableHeaders |
Out-String
).Split( [System.Environment]::NewLine,
[System.StringSplitOptions]::RemoveEmptyEntries ) |
ForEach-Object { $_.Trim() } |
Sort-Object -Unique
- U+002D DashPunctuation Hyphen-Minus
− U+2212 MathSymbol Minus Sign
* U+002A OtherPunctuation Asterisk
/ U+002F OtherPunctuation Solidus
+ U+002B MathSymbol Plus Sign
× U+00D7 MathSymbol Multiplication Sign
÷ U+00F7 MathSymbol Division Sign
<#
_get-CharInfo_1.1.ps1
Origin by: http://poshcode.org/5234
sorry, the above link is not available; last time checked 2018-05-07
Improved by: https://stackoverflow.com/users/3439404/josefz
Use this like this: "ábč",([char]'x'),0xBF | Get-CharInfo
Activate dot-sourced like this (apply a real path instead of .\):
. .\_get-CharInfo_1.1.ps1
#>
Add-Type -Name UName -Namespace Microsofts.CharMap -MemberDefinition $(
switch ("$([System.Environment]::SystemDirectory -replace
'\\', '\\')\\getuname.dll") {
{Test-Path -LiteralPath $_ -PathType Leaf} {@"
[DllImport("${_}", ExactSpelling=true, SetLastError=true)]
private static extern int GetUName(ushort wCharCode,
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder buf);
public static string Get(char ch) {
var sb = new System.Text.StringBuilder(300);
UName.GetUName(ch, sb);
return sb.ToString();
}
"@
}
default {'public static string Get(char ch) { return "???"; }'}
})
function Get-CharInfo {
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject],[System.Array])]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
$InputObject
)
begin {
Set-StrictMode -Version latest
function out {
param(
[Parameter(Position=0, Mandatory=$true )] $ch,
[Parameter(Position=1, Mandatory=$false)]$nil=''
)
if (0 -le $ch -and 0xFFFF -ge $ch) {
[pscustomobject]@{
Char = [char]$ch
CodePoint = 'U+{0:X4}' -f $ch
Category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($ch)
Description = [Microsofts.CharMap.UName]::Get($ch)
}
} elseif (0 -le $ch -and 0x10FFFF -ge $ch) {
$s = [char]::ConvertFromUtf32($ch)
[pscustomobject]@{
Char = $s
CodePoint = 'U+{0:X}' -f $ch
Category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($s, 0)
Description = '???' + $nil
}
} else {
Write-Warning ('Character U+{0:X} is out of range' -f $ch)
}
}
}
process {
if ($PSBoundParameters['Verbose']) {
Write-Warning "InputObject type = $($InputObject.GetType().Name)"}
if ($null -cne ($InputObject -as [char])) {
#Write-Verbose "A $([char]$InputObject) InputObject character"
out $([int][char]$InputObject) ''
} elseif ($InputObject -isnot [string] -and $null -cne ($InputObject -as [int])) {
#Write-Verbose "B $InputObject InputObject"
out $([int]$InputObject) ''
} else {
$InputObject = [string]$InputObject
#Write-Verbose "C $InputObject InputObject.Length $($InputObject.Length)"
for ($i = 0; $i -lt $InputObject.Length; ++$i) {
if ( [char]::IsHighSurrogate($InputObject[$i]) -and
(1+$i) -lt $InputObject.Length -and
[char]::IsLowSurrogate($InputObject[$i+1])) {
$aux = ' 0x{0:x4},0x{1:x4}' -f [int]$InputObject[$i],
[int]$InputObject[$i+1]
Write-Verbose "surrogate pair $aux at position $i"
out $([char]::ConvertToUtf32($InputObject[$i], $InputObject[1+$i])) $aux
$i++
} else {
out $([int][char]$InputObject[$i]) ''
}
}
}
}
}