Discuss Scratch
- Discussion Forums
 - » Help with Scripts
 - » Any ideas on how to sort a list of values?
        
         
- JaredM18
 - 
                            
						
						
                            Scratcher
                        
						
						 
19 posts
Any ideas on how to sort a list of values?
My brain is absolutely fried at this point. I've got a list with certain values that I need to sort from highest to lowest, but I'm not sure how to get it done. This seems like a simple thing to do but I don't know where to start. Anyone have any ideas?
                        
                        
                    - CoconutGator
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Any ideas on how to sort a list of values?
I believe this just got answered recently here: https://scratch.mit.edu/discuss/topic/607728/
Is that solution they offered what you mean?
                        
                        
                    Is that solution they offered what you mean?
- deck26
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Any ideas on how to sort a list of values?
There are various sorting methods in computing and which is best depends on the size of the list and other factors. You'll find lots of info online.
If the list isn't already populated one method is the insertion sort where you just insert each new value in the right place to start with rather than sorting later.
Otherwise the bubble sort is one of the simplest. Loop through the list comparing adjacent values and swap them if they are in the wrong order. Repeat until you loop through the list and don't make any swaps. For short lists this is OK.
                        
                        
                    If the list isn't already populated one method is the insertion sort where you just insert each new value in the right place to start with rather than sorting later.
Otherwise the bubble sort is one of the simplest. Loop through the list comparing adjacent values and swap them if they are in the wrong order. Repeat until you loop through the list and don't make any swaps. For short lists this is OK.
- musicROCKS013
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Any ideas on how to sort a list of values?
I haven't tested this, but would this work? It takes list and sorts it into highest to lowest into list2.
                        
                            repeat until <(length of [list v] :: list) = [0]>
set [highest v] to ((-1) / (0))
set [listnumber v] to []
set [loop v] to [0]
repeat (length of [list v] :: list)
change [loop v] by (1)
if <(item (loop) of [list v] :: list) > (highest)> then
set [listnumber v] to (loop)
set [highest v] to (item (loop) of [list v] :: list)
end
add (item (listnumber) of [list v] :: list) to [list2 v]
delete (listnumber) of [list v]
end
end
Last edited by musicROCKS013 (May 26, 2022 15:26:25)
- Discussion Forums
 - » Help with Scripts
 - 
            » Any ideas on how to sort a list of values? 
         
            


