Jump to content
Tommi Prami

"natural" compare function for sort

Recommended Posts

Hello is there any implementation for compare function for ordering strings with numbers into numeric order:

Random list to this:

1 test

2 test

10 test

11 test

20 test

21 test
test 1

test 2

test 10

test 11

test 12

test 20

 

etc,

Edited by Tommi Prami

Share this post


Link to post
2 minutes ago, FPiette said:

Are you showing the list sorted as you like or is it the unsorted list?

Sorry bad example. Edited the list a bit.

Not just alphabetically but numbers would always be Smaller or larger first (depending if Ascending or Descending sort).

 

Can't remeber which this was called. Maybe natural sort etc.,.,

Share this post


Link to post

If you like my answer, you should like it (The heart icon on the answer bottom right). Thanks.

 

  • Like 1

Share this post


Link to post

TStringHelper.Compare has an overload where you can give the option TCompareOption.coDigitAsNumbers for this purpose.

  • Like 4
  • Thanks 1

Share this post


Link to post
3 hours ago, FPiette said:

If you like my answer, you should like it (The heart icon on the answer bottom right). Thanks.

 

Seems a bit needy

Share this post


Link to post

Hi Team,

I have been following this thread because I have a need.

What would be the correct natural sort order for these?

'file10.txt', 'File2.txt', '2fileb.txt', 'file1.txt', '1FileA.txt', 'file20.txt', '12fiLe.txt', 'file11.txt', '10file.txt'

Is there a reference for dictating Natural Sort Order rules?

I have my own StringArray Natural Sorting routines and they seem to work.  I just want to make sure they are giving me the correct result IAW NSO rules.

This is what I am producing att:

image.thumb.png.fdd83fb0d14bc55381b08573b411553d.png

 

Regards & TIA,

Ian

Edited by Ian Branch

Share this post


Link to post

@Ian Branch

 

I think that the Natural Order (to computer) is based on "symbol's" numerical value on the table used not? (normally ASCII table)

 

Which would come first for you: the letter "a=97 / A=65" (lowercase or uppercase), or the number "0 = 48" (zero)?

 

image.thumb.png.0e817e7418c731e532b50b44c6a998b5.png   me(...) X StrCmpLogicalW(...)

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

Hi Team,

I redid my routine to use StrCmpLogicalW.  Now I get...

image.thumb.png.d92e9eabc810e6a011b392eefba6036a.png

uses System.Generics.Defaults;

procedure SortStringArray(var AStringArray: TArray<string>);
var
  Comparer: IComparer<string>;
begin
    Comparer := TComparer<string>.Construct(
      function(const ALeft, ARight: string): Integer
      begin
        Result := StrCmpLogicalW(PChar(ALeft.ToLower), PChar(ARight.ToLower));
      end
    );

  // Sort the string array using the custom comparer
  TArray.Sort<string>(AStringArray, Comparer);
end;

 

Regards & Tks.

 

Ian

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×