viewpoint-particle

Author Topic: reverse sort  (Read 444 times)

pguild

  • Jr. Member
  • **
  • Posts: 87
  • The dog is always right!
    • MasteryLearning
reverse sort
« on: March 18, 2023, 11:52:45 am »
It would be helpful if there were an argument in Itemsort to ask for a reverse sort. But in the mean time, how can I roll my own reverse sort?

stanl

  • Pundit
  • *****
  • Posts: 1799
Re: reverse sort
« Reply #1 on: March 19, 2023, 07:55:24 am »
Feel you would have to indicate the list items you want to sort. The WB function is char-based so in the example below not really going to sort by date with just the itemsort() function. There are,of course methods to get around treating numbers as numbers or dates as dates and possibly some RegEx, Array, or map code would help. My response is based on a generic reverse-sort holy grail and as first indicted, if you post a list, might be an easy solution.


Code: Winbatch

list="10/10/2022,01/3/2022,03/1/2023,10/5/2023,04/12/2023"
newlist = ItemSort(list, ",")
Message("List generated by ItemSort", newlist)
 

JTaylor

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
Re: reverse sort
« Reply #2 on: March 19, 2023, 08:44:40 am »
As Stan noted there are types that won't work, but that aside, use ItemSort() and then rebuild the list using a For Loop, starting from the end.  Not trying for usable code below, just to make the idea clear.

nlist = ""
list = ItemSort(list,",")
cnt = ItemCount(list,",")
For x = cnt to 1 by -1
   nlist = nlist:",":ItemExtract(x,list,",")
Next

nList = ItemRemove(1,nlist,",")


Haven't tried it but you might be able to load to an array and sort.  I think it handles some types.  Would need to look at documentation.  Arrayize() would be the place to start.

Hopefully WB will add this option.  It is something I requested as well, many years ago.

Jim

kdmoyers

  • Sr. Member
  • ****
  • Posts: 500
Re: reverse sort
« Reply #3 on: March 20, 2023, 06:16:48 am »
Just for grins, I threw the problem at ChatGPT4.  It's a little off on syntax, but amazing nontheless.

PROMPT:
in Winbatch programming language, write a routine to reverse a sorted list A.

RESPONSE:
WinBatch is a scripting language primarily used for automating Windows tasks. Here's a simple routine to reverse a sorted list in WinBatch:

Code: Winbatch
; Function to reverse a sorted list A
:UserFunction ReverseList(A)
    listLength = ItemCount(A, @TAB) ; Get the length of the list
    reversedList = "" ; Initialize the reversed list

    ; Loop through the list in reverse order
    for i = listLength - 1 to 0 step -1
        element = ItemExtract(i, A, @TAB) ; Extract the i-th element
        reversedList = StrCat(reversedList, element, @TAB) ; Append the element to the reversed list
    next

    return StrTrim(reversedList, 3, @TAB) ; Remove the trailing tab and return the reversed list
:EndFunction

; Example usage
sortedList = "1@TAB2@TAB3@TAB4@TAB5"
reversedList = ReverseList(sortedList)

Message("Reversed List", StrReplace(reversedList, @TAB, ", ")) ; Display the reversed list, replacing tabs with commas

 
This script defines a function ReverseList() that takes a tab-separated list A as input, reverses it, and returns the reversed list. The example usage demonstrates how to use this function to reverse a sorted list and display the result.
The mind is everything; What you think, you become.

kdmoyers

  • Sr. Member
  • ****
  • Posts: 500
Re: reverse sort
« Reply #4 on: March 20, 2023, 06:27:10 am »
I wonder where it got this odd syntax from?
like, the ":UserFunction" thing and the three parameter StrTrim function call?
The mind is everything; What you think, you become.

JTaylor

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
Re: reverse sort
« Reply #5 on: March 20, 2023, 07:21:47 am »
You probably need to feed it the documentation first :)

Jim

I wonder where it got this odd syntax from?
like, the ":UserFunction" thing and the three parameter StrTrim function call?

td

  • Tech Support
  • *****
  • Posts: 4352
    • WinBatch
Re: reverse sort
« Reply #6 on: March 20, 2023, 07:32:11 am »
The end is nigh...

Anyway, why not load the data into an array? The ArraySort function has options for sorting direction (@Ascending, @Descending) and sort type (@Stringsort, @Logicalsort). If you need an item list, you can use the Arrayize and ArrayItemize functions to convert from a list to an array and back again.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

  • Sr. Member
  • ****
  • Posts: 500
Re: reverse sort
« Reply #7 on: March 20, 2023, 08:59:32 am »
You're right Tony, that's tough to beat for simplicity:
Code: Winbatch
x = arrayize(list,@tab)
arraysort(x,@descending)
newlist = arrayitemize(x, @tab)
The mind is everything; What you think, you become.

td

  • Tech Support
  • *****
  • Posts: 4352
    • WinBatch
Re: reverse sort
« Reply #8 on: March 20, 2023, 01:15:18 pm »
A date example not practical for large data sets but you should not be using lists for large data sets.

Code: Winbatch
list="10/10/2022,01/3/2022,03/1/2023,10/5/2023,04/12/2023"
arr = Arrayize(list, ",")
last = ArrInfo(arr, 1) - 1
; Convert to WIL time.
for i = 0 to last
   arr[i] = ObjectType("date", arr[i])
next
ArraySort(arr,@DESCENDING|@LOGICALSORT)
; Convert time back to original format
for i = 0 to last
   arr[i] = TimeFormat(arr[i], "MM/d/yyyy")
next
newlist = ArrayItemize(arr, ",")
Message("List generated by ItemSort", newlist)
exit
 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1799
Re: reverse sort
« Reply #9 on: March 24, 2023, 07:40:22 am »
I know this is off-topic. But went back into my 2006-2009 archives. Then I used a fabricated recordset to both try to identify date <> numeric <> or text items then use the .sort method with either Ascending or Descending.

td

  • Tech Support
  • *****
  • Posts: 4352
    • WinBatch
Re: reverse sort
« Reply #10 on: March 24, 2023, 01:26:08 pm »
Database tools should be and in many cases are well-suited for sorting tasks of various data types.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1799
Re: reverse sort
« Reply #11 on: March 25, 2023, 04:07:14 am »
Then there is .NET System.Collections  Arraylist...   but not sure if it plays nice with WB CLR :-\ :-\ :-\


EDIT:  I dredged this up from 2013. Didn't get as far as the sort() method
Code: Winbatch

;Winbatch 2013 - CLR ArrayList
;
;
;Stan Littlefield June 4, 2013
;////////////////////////////////////////////////////////////////////////////////////////////////////////


ObjectClrOption("useany","System.Net")
oList = ObjectClrNew("System.Collections.ArrayList")
oList.Add("Hello")  
oList.Add("World")  
oList.Add("!")  
 
cTxt="My ArrayList":@CRLF
cTxt=cTxt:"Count: ":oList.Count:@CRLF  
cTxt=cTxt:"Capacity: ":oList.Capacity:@CRLF
cTxt=cTxt:"Fixed Len?: ":oList.IsFixedSize:@CRLF  
cTxt=cTxt:"Read only?: ":oList.IsReadOnly:@CRLF
cTxt=cTxt:"Item 1: ":oList.Item(1):@CRLF
oList=0
Message("",cTxt)
Exit  
 

pguild

  • Jr. Member
  • **
  • Posts: 87
  • The dog is always right!
    • MasteryLearning
Re: reverse sort
« Reply #12 on: March 25, 2023, 01:15:38 pm »
Thanks for the idea. I had no idea you could use arrays in this way.   :o
Here's a function I made along with a test.
I like to pass the delimiter to the function so I can use various delimiters in my lists.

#definefunction ReverseSort (list,delim)
  x = arrayize(list,delim)
  arraysort(x,@descending)
  List = arrayitemize(x, delim)
  Return(List)
#Endfunction

;;;;;;;;;;;;;;;;;;;;;main;;;;;;;;;;;;;;;;;;;;;;;;;;;
list = "zebra,connie,beth,able"
NewList = ReverseSort(list,",")
Pause(List,Newlist)
RETURN







You're right Tony, that's tough to beat for simplicity:
Code: Winbatch
x = arrayize(list,@tab)
arraysort(x,@descending)
newlist = arrayitemize(x, @tab)

stanl

  • Pundit
  • *****
  • Posts: 1799
Re: reverse sort
« Reply #13 on: March 26, 2023, 08:12:28 am »
Great! Sorry went of tangent on data types.