- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有大量数据,计算排序键的成本相当高。我想要做的是使用 DSU 模式,我在其中获取行并计算排序键。一个例子:
Qty Name Supplier
Row 1: 50 Widgets IBM
Row 2: 48 Thingies Dell
Row 3: 99 Googaws IBM
要按数量和供应商排序,我可以使用排序键:0050 IBM
、0048 Dell
、0099 IBM
。数字右对齐,文本左对齐,根据需要填充所有内容。
如果我需要按降序顺序对数量进行排序,我只需从常量(例如 10000)中减去该值来构建排序键:9950 IBM
, 9952 戴尔
, 9901 IBM
。
如何快速/廉价地为 C# 中的字母 字段构建降序键?
[我的数据全部是带有 ISO 8859 扩展字符的 8 位 ASCII。]
注意:在 Perl 中,这可以通过 bit-complementing the strings 来完成:
$subkey = $string ^ ( "\xFF" x length $string );
将此解决方案直接移植到 C# 中不起作用:
subkey = encoding.GetString(encoding.GetBytes(stringval).
Select(x => (byte)(x ^ 0xff)).ToArray());
我怀疑是因为在 C#/Perl 中处理字符串的方式不同。也许 Perl 正在按 ASCII 顺序排序,而 C# 正在尝试变聪明?
下面是一段尝试实现此目的的示例代码:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
List<List<string>> sample = new List<List<string>>() {
new List<string>() { "", "apple", "table" },
new List<string>() { "", "apple", "chair" },
new List<string>() { "", "apple", "davenport" },
new List<string>() { "", "orange", "sofa" },
new List<string>() { "", "peach", "bed" },
};
foreach(List<string> line in sample)
{
StringBuilder sb = new StringBuilder();
string key1 = line[1].PadRight(10, ' ');
string key2 = line[2].PadRight(10, ' ');
// Comment the next line to sort desc, desc
key2 = encoding.GetString(encoding.GetBytes(key2).
Select(x => (byte)(x ^ 0xff)).ToArray());
sb.Append(key2);
sb.Append(key1);
line[0] = sb.ToString();
}
List<List<string>> output = sample.OrderBy(p => p[0]).ToList();
return;
最佳答案
你可以到达你想要的地方,尽管我承认我不知道是否有更好的整体方式。
直接翻译 Perl 方法的问题是 .NET 根本不允许您对编码如此放任。但是,如果正如您所说,您的数据都是可打印的 ASCII(即由 Unicode 代码点在 32..127 范围内的字符组成)——请注意,没有“8 位 ASCII”这样的东西——那么你可以这样做:
key2 = encoding.GetString(encoding.GetBytes(key2).
Select(x => (byte)(32+95-(x-32))).ToArray());
在这个表达式中,我已经明确说明了我在做什么:
x
(我假设它在 32..127 中)它不是很好,但确实有效。
关于c# - Decorate-Sort-Undecorate,如何按降序对字母字段进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135500/
我在使用 may e(fx)clipse 应用程序时遇到问题。我想在应用程序启动时显示启动屏幕。我成功创建了实现 StartupProgressTrackerService 的类,并调用了我的 sta
我一直在尝试 Tornadofx。尝试创建自定义标题栏,这是我目前正在尝试的代码 fun main(args: Array) { launch(args) } class MyApp : Ap
对于一个科学实验,我写了一个turtle.py ,它会打开一个 800x480 的窗口并绘制一个缓慢增长的黑点。 turtle.py以 C:\Users\kaza>python C:\Users\ka
我有大量数据,计算排序键的成本相当高。我想要做的是使用 DSU 模式,我在其中获取行并计算排序键。一个例子: Qty Name Supplier Row 1: 5
当我链接我的 C++ DLL 程序时,我收到以下错误和警告: Severity Code Description Project File Line Suppressi
Python 爱好者喜欢谈论一种称为 DSU 的技术: 假设我想按第三个字段的 int 值对列表进行排序: # Decorate decorated = [(int(item[2]), item) f
我使用 JavaFX 创建了一个没有标题栏的全屏应用程序,但它仅在 Windows 上以全屏模式成功启动。当我在Linux(Ubuntu 19.04 Disco Dingo)上尝试时,如果没有标题栏,
我用 Java 创建了一个未修饰的每像素半透明 JFrame。框架的大小(在下面的屏幕中)是 512x512。到目前为止,我只添加了 3 张图像。我的问题是 Compton 创建的窗口阴影几乎破坏了逐
我正在尝试测试一个组件取消装饰器,但我没有。 例子: import React, { Component } from 'react' import { connect } from 'react-r
我最近更新到了 Angular v9,并在更改日志中定义,不推荐使用未修饰的基类,该基类使用 Angular 功能或由指令或组件扩展。 所以我的应用程序中有很多 mixin 是这样的: 销毁: exp
我正在尝试创建一个不会出现在 Windows 任务栏上且未装饰的舞台(没有边框,也没有关闭/最小化/最大化按钮)。我的最终目标是创建一个会弹出通知窗口的托盘图标应用程序。 与此类似 question我
我是一名优秀的程序员,十分优秀!