Tommi Prami 130 Posted December 22, 2020 (edited) 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 December 22, 2020 by Tommi Prami Share this post Link to post
FPiette 383 Posted December 22, 2020 Are you showing the list sorted as you like or is it the unsorted list? 1 Share this post Link to post
Tommi Prami 130 Posted December 22, 2020 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
Tommi Prami 130 Posted December 22, 2020 This -> https://en.wikipedia.org/wiki/Natural_sort_order Share this post Link to post
FPiette 383 Posted December 22, 2020 Look there: https://stackoverflow.com/questions/5134712/how-to-get-the-sort-order-in-delphi-as-in-windows-explorer 3 Share this post Link to post
Tommi Prami 130 Posted December 22, 2020 15 minutes ago, FPiette said: Look there: https://stackoverflow.com/questions/5134712/how-to-get-the-sort-order-in-delphi-as-in-windows-explorer Thanks. That API works if one is using Windows, as we are. If someone has code for this, pleace share, I think would be usable for others tool. -Tee- Share this post Link to post
FPiette 383 Posted December 22, 2020 If you like my answer, you should like it (The heart icon on the answer bottom right). Thanks. 1 Share this post Link to post
Uwe Raabe 2057 Posted December 22, 2020 TStringHelper.Compare has an overload where you can give the option TCompareOption.coDigitAsNumbers for this purpose. 4 1 Share this post Link to post
David Heffernan 2345 Posted December 22, 2020 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
Mike Torrettinni 198 Posted December 22, 2020 5 hours ago, FPiette said: Look there: https://stackoverflow.com/questions/5134712/how-to-get-the-sort-order-in-delphi-as-in-windows-explorer Thanks! Pretty cool example with TStringList, will use it. 1 Share this post Link to post
Anders Melander 1783 Posted December 22, 2020 Googling "delphi natural sort" would have gotten you the answer. Share this post Link to post
aehimself 396 Posted December 22, 2020 9 hours ago, David Heffernan said: Seems a bit needy Seems to be offtopic. 1 Share this post Link to post
programmerdelphi2k 237 Posted June 26, 2023 (edited) @Tommi Prami in time, (almost 3 year is nothing... isn't) my sample no-dependences of DLL, then you can use it in any platform! https://en.delphipraxis.net/topic/9276-my-tstringlist-custom-sorting-trying-to-mimic-windows-explorer-way/ Edited June 26, 2023 by programmerdelphi2k 1 Share this post Link to post
Ian Branch 127 Posted June 27, 2023 (edited) 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: Regards & TIA, Ian Edited June 27, 2023 by Ian Branch Share this post Link to post
programmerdelphi2k 237 Posted June 27, 2023 (edited) @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)? me(...) X StrCmpLogicalW(...) Edited June 28, 2023 by programmerdelphi2k 1 Share this post Link to post
Ian Branch 127 Posted June 28, 2023 Hi Team, I redid my routine to use StrCmpLogicalW. Now I get... 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