Delphi 如何根据特定规则对TStringList进行排序?
我有一个TStringList,它有很多行字符串 我现在要做的是根据TStringList中的哪个列,根据特定的值对这个TStringList进行排序 比如说 adsfoiadjfoaidjfoaidfjo BABABA asdfsd 0101010 skfjlgjljkglsdkgjasaasd BABABA dafdff 0419299 asdaksdjakbkj3409560295 BABABA kjfhan 0923858 dsdddsdasdadasdasdasdgg BABABA dafdff 0419299 45645654654654654654666 BABABA dafdff 0489421 dsdddsdasdadasdasdasdgg CACACA dafdff 0419299 正如您可以看到的,第二行和第四行具有相同的BABABA值,在行尾也具有相同的数字,第五行具有相同的BABABA值,但不具有相同的数字。第六排有相同的数字,但不是相同的巴巴巴 我想对它们进行排序的正确方法是 adsfoiadjfoaidjfoaidfjo BABABA asdfsd 0101010 skfjlgjljkglsdkgjasaasd BABABA dafdff 0419299 dsdddsdasdadasdasdasdgg BABABA dafdff 0419299 45645654654654654654666 BABABA dafdff 0489421 asdaksdjakbkj3409560295 BABABA kjfhan 0923858 dsdddsdasdadasdasdasdgg CACACA dafdff 0419299Delphi 如何根据特定规则对TStringList进行排序?,delphi,sorting,tstringlist,Delphi,Sorting,Tstringlist,我有一个TStringList,它有很多行字符串 我现在要做的是根据TStringList中的哪个列,根据特定的值对这个TStringList进行排序 比如说 adsfoiadjfoaidjfoaidfjo BABABA asdfsd 0101010 skfjlgjljkglsdkgjasaasd BABABA dafdff 0419299 asdaksdjakbkj3409560295 BABABA kjfhan 0923858 dsdddsdasdadasdasdasdgg BABABA d
我想首先在25-30列之后排序,如果有匹配的数字,也要对数字进行排序。如果数字不匹配,则应在BABABA之后进行排序。如果BABABA不匹配,则可以以任何方式进行排序。定义自己的比较函数,从字符串中选择要用于排序的部分:
function MyCompare(List: TStringList; Index1, Index2: Integer): Integer;
function GetOrderingSubString(const s: string): string;
begin
Assert(Length(s)=45);
Result := Copy(s, 25, 6) + Copy(s, 39, 7);
end;
var
s1, s2: string;
begin
s1 := GetOrderingSubString(List[Index1]);
s2 := GetOrderingSubString(List[Index2]);
Result := CompareText(s1, s2); // or CompareStr, you decide
end;
并将该函数传递给CustomSort:
定义自己的比较函数,从字符串中选择要用于排序的部分:
function MyCompare(List: TStringList; Index1, Index2: Integer): Integer;
function GetOrderingSubString(const s: string): string;
begin
Assert(Length(s)=45);
Result := Copy(s, 25, 6) + Copy(s, 39, 7);
end;
var
s1, s2: string;
begin
s1 := GetOrderingSubString(List[Index1]);
s2 := GetOrderingSubString(List[Index2]);
Result := CompareText(s1, s2); // or CompareStr, you decide
end;
并将该函数传递给CustomSort:
因此,首先要对第二列进行排序,然后对第四列进行排序。那你需要 从有关函数类型的帮助中: Index1和Index2是列表中要比较的项目的索引。 回调返回: 如果Index1标识的字符串位于Index2标识的字符串之前,则值小于0 如果两个字符串相等,则为0 如果Index1的字符串位于Index2标识的字符串之后,则该值大于0。 因此,如果首先在第二列进行比较,当相等时,继续在第四列进行比较。假设所有文本都大写,所有字符串大小相同,语法相同,那么这应该可以:
function CompareItems(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := CompareStr(Copy(List[Index1], 25, 6), Copy(List[Index2], 25, 6));
if Result = 0 then
Result := CompareStr(Copy(List[Index1], 39, 7), Copy(List[Index2], 39, 7));
end;
用法:
List.CustomSort(CompareItems);
因此,首先要对第二列进行排序,然后对第四列进行排序。那你需要 从有关函数类型的帮助中: Index1和Index2是列表中要比较的项目的索引。 回调返回: 如果Index1标识的字符串位于Index2标识的字符串之前,则值小于0 如果两个字符串相等,则为0 如果Index1的字符串位于Index2标识的字符串之后,则该值大于0。 因此,如果首先在第二列进行比较,当相等时,继续在第四列进行比较。假设所有文本都大写,所有字符串大小相同,语法相同,那么这应该可以:
function CompareItems(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := CompareStr(Copy(List[Index1], 25, 6), Copy(List[Index2], 25, 6));
if Result = 0 then
Result := CompareStr(Copy(List[Index1], 39, 7), Copy(List[Index2], 39, 7));
end;
用法:
List.CustomSort(CompareItems);
非常感谢你的帮助!非常感谢你的帮助!