| |

VerySource

 Forgot password?
 Register
Search
View: 844|Reply: 3

Sorting problem

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

Post time: 2020-2-14 08:00:01
| Show all posts |Read mode
The fool asked a silly question:
Array strlist1: TStringList; StrList [i]> '0'
How to sort the values ​​in strlist1 in ascending order, with the value of '0' following. ???????
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-12 13:45:01
| Show all posts
strlist1.sort
Reply

Use magic Report

0

Threads

25

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-4-25 14:00:01
| Show all posts
The fool asks a silly question:
============
Smart people answer smart questions. :) :)

function Compare (List: TStringList; Index1, Index2: Integer): Integer;
begin
  if List [Index1] = '0' then
    if List [Index2] <> '0' then
      Result: = 1
    else
      Result: = 0
  else
    if List [Index2] = '0' then
      Result: = -1
    else
      Result: = CompareText (List [Index1], List [Index2]);
end;

procedure TFormDemo.ButtonDemoClick (Sender: TObject);
var
  SL: TStringList;
begin
  SL: = TStringList.Create;
  SL.Add ('0');
  SL.Add ('4');
  SL.Add ('1');
  SL.Add ('0');
  SL.Add ('0');
  SL.Add ('5');
  SL.Add ('3');
  SL.Add ('2');
  SL.CustomSort (@Compare);
  ListBox.Items: = SL; // Look at the result
end;

//result
======
1
2
3
4
5
0
0
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-28 15:30:01
| Show all posts
Normal sorting must be 0 in front,
The landlord uses SL.CustomSort (@Compare); custom sorting, so the sorting result is related to your Compare method
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list