Jump to content

order numbers


Guest rookie programmer
 Share

Recommended Posts

Guest rookie programmer

Hello at all,

I would like to order numbers which you can find in a text file.

e.g. : 5 -> 5

23 -> 7

12 -> 12

7 -> 23

46 -> 46

How can I do that ???

Link to comment
Share on other sites

Guest Rookie-Programmer

ok, nobody has answered.

I try to explain it again.

I have a textfile.

line 1:12

line 2:2

line 3:25

line4:4

I would like to sort these numbers in the correct order.

line 1:2

line 2:4

line 3:12

line 4:25

:D

Link to comment
Share on other sites

You need to create what is called a sorting function. There are several methods to create a sorting function, and you might find a related topic about alphabetizing values useful. That thread talks about several kinds of sorting methods, and gives some examples of how they are used.

You can also google search for the following keywords to get more information on various sorting methods:

  • bubble sort
  • shell sort
  • heap sort
  • selection sort
  • insertion sort
Remember, when searching on the web for a phrase, enclose the phrase in double quotes.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

You might also find this document helpful, which goes into very good detail about sorting, and talks about all the basic sorting types. Normally I'd have let you find it (or something similar), but I had to look up one of these anyway, so here it is.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

You might also find this document helpful, which goes into very good detail about sorting, and talks about all the basic sorting types.  Normally I'd have let you find it (or something similar), but I had to look up one of these anyway, so here it is.

Interesting page.... :D Now I can see graphically the efficiency between these algorithms. :huh2:
Link to comment
Share on other sites

Guest Guest

thank you for your help but I´ve another problem.

If I compare e.g. 3 with 10, the program will think that 10 is less than 3.

If I compare e.g. 12 with 14, the program will think that 12 is less than 14.

The program thinks that numbers less than 10 are greater than numbers over 10.

$number=1
$D=0
$B=1
 
 $smallest = FileReadLine("test1.txt",1)
  while $D=0
 $number=$number+1
 $lap = FileReadLine("test1.txt",$number)
 FileWriteLine("test2.txt",$smallest)
 FileWriteLine("test2.txt",$lap)
 if $smallest > $lap then $B=0
  while $B=0 
   $smallest = $lap
   $B=1
  WEnd
   if $number > 20 then Exitloop
   $B=1 
 WEnd


            
        

        

        
    

    

    




    Link to comment
    
        
    
    
    

    
    Share on other sites
    

    
        
            

    

        
            

    

        
            

    

        
            

    

        
    


    
    More sharing options...

    


    

                    
                    
                    
                

                    

                    
                    





    

    

    
        
            
                

    
        
    

                
                
            
        
        
            
                


Guest rookie programmer
            
            
                Posted 
                
            
        
    
    
        


Guest rookie programmer
            
        
        
            
                
                    

    
        
    

                    
                    
                
            
            
                Guests
                
            
            
            
        
    
    
        



    
        
            
                
                    
                    
                    
                    
                    
                
            
            
                
                    
                    
                        
                        
                            Share
                        
                        
                        
                        
                        
                            
                                
                            
                            
                            
                            
                            
                            
                        
                    
                
                
            
        

        
            Posted 
            
            
                
                
            
        
    

    

    

    
        
        
            I´ve found the problem.

The program thinks that 3 is the number 30. I don´t know why.

e.g. a textfile:

3
23
12
5

result: 12 because "30",23,"50" are greater. If I change the 3 in 03 the program will do the work correctly.


            
        

        

        
    

    

    




    Link to comment
    
        
    
    
    

    
    Share on other sites
    

    
        
            

    

        
            

    

        
            

    

        
            

    

        
    


    
    More sharing options...

    


    

                    
                    
                    
                

                    

                    
                    





    

    

    
        
            
                


    
        
    

                
                
                    
                        

                    
                
            
        
        
            
                


pekster
            
            
                Posted 
                
            
        
    
    
        


pekster
            
        
        
            
                
                    


    
        
    

                    
                    
                        

                    
                
            
            
                Active Members
                
            
            
                
                    
                        
                            
                                
                            
                                 887
                            
                                
                            
                        
                        
                    
                
            
            
                

    
    
        
Newfound chai tea convert
    
    

            
        
    
    
        



    
        
            
                
                    
                    
                    
                    
                    
                
            
            
                
                    
                    
                        
                        
                            Share
                        
                        
                        
                        
                        
                            
                                
                            
                            
                            
                            
                            
                            
                        
                    
                
                
            
        

        
            Posted 
            
            
                
                    (edited)
                
                
            
        
    

    

    

    
        
        
            Assuming you're only dealing with numbers, this UDF should do the trick.  It's a spin-off of the bubble sort, and puts largets numbers at the bottom.  Just don't use it with lists over about 5000 elements and you'll be fine.  Please note that if you include any strings, they will be interperted as all 0's (in no particular order.)#include-once
Func NumSort(ByRef $InArray)
  If NOT IsArray($InArray) Then
    SetError(1)
    return 0
  EndIf
  Local $i, $j, $index
  For $i = 0 To UBound($InArray) - 1
    $InArray[$i] = Number($InArray[$i])
  Next
  For $i = UBound($InArray) - 1 To 1 Step -1
    $index = 0
    For $j = 1 To $i
      If $InArray[$j] > $InArray[$index] Then $index = $j
    Next
    $j = $InArray[$i]
    $InArray[$i] = $InArray[$index]
    $InArray[$index] = $j
  Next
  SetError(0)
  return 1
EndFunc

Edited to fix several problems in my code

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

I´ve found the problem.

The program thinks that 3 is the number 30. I don´t know why.

The reason is that the sorts on the "alphabetical sort" thread are truely an alphabetical sort, and 3 appears as greater than 12 just as 'C' comes after 'AB' when sorting an alphabetical list. If you're only dealing with numbers, see my previous code.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Sorry, my example had about 3 problems. They have all been corrected, and the code works.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

...

I have a textfile.

line 1:12

line 2:2

line 3:25

line4:4

I would like to sort these numbers in the correct order.

...

Hi rookie programmer,

The easiest and fastest way is to do it on the commandline or batchfile.

one line of code is enought.

sort.exe -n < unsorted.txt > sorted.txt

-n is for sorting numbers

Sort.exe is inside this package:

Native Win32 ports of some GNU utilities

It's free, powerfull and small

The unxutils consist of small exe-files which can used standalone.

They all are standard unix/linux commands

This package is a MUST for everyone scripting under Windows.

arctor

Link to comment
Share on other sites

This package is a MUST for everyone scripting under Windows.

This looks like a handy file, but sometimes it is not an option to include a file with your scripts. If a script is to be used on more than one computer, you would have to FileInstall the exe to use it. In some cases, you may not have write access to the directory where the script is running from or may not be allowed to dump files elsewhere on the system, so you would have to exit with a failure.

However, in cases where you are only using a script for yourself or set conditions where you are assured you could extract it, this app could become helpful.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

thank you for your help but I´ve another problem.

If I compare e.g. 3 with 10, the program will think that 10 is less than 3.

If I compare e.g. 12 with 14, the program will think that 12 is less than 14.

The program thinks that numbers less than 10 are greater than numbers over 10.

The problem is that you are comparing strings not numbers. The character "1" comes before "3", so "10" < "3". Convert your strings to numbers as you store them in the array. See the build-in function Number in the help file.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

The problem is that you are comparing strings not numbers.  The character "1" comes before "3", so "10" < "3".  Convert your strings to numbers as you store them in the array.  See the build-in function Number in the help file.

He's right. You'll notice if you look in my example code above that I converted each value to test into a number before testing. If you would like you can include my function, set up an array from the numbers in your text file, and call NumSort($array)

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Hi Pekster,

...but sometimes it is not an option to include a file with your scripts.

Ok, I agree with some things you said. But not with all.

First of all: "rookie programmer" had a problem with sorting numbers. It sounded itself that he was searching for any solution, not particularly with AutoIt. So I gave the hint.

Basically I also like to have an all-in-one script or a compiled exe.

But sometimes it's faster to make, faster to run and less complicated, to use small helpers. When I can do it in one line, i don't need to write a function of 10, 20 or more lines what is mostly slower than the tool which was built exactly for that one job. I don't have the requirement, to make all stuff with AutoIt only.

To say it with the words of Timo Salmi, one of the Batch Gurus:

...One has to draw the line somewhere, stop kidding oneself, and start using batch enhancers (external programs to help out)...

(This is related to DOS/WINDOWS batch-language not to AutoIt.)

But I see it similarly.

For example: I did a AutoIt script to search and get data from a mysql database using the standard mysql commandline client. It works fast and well and I'm not sad about, that AutoIt has no mysql functions.

I hope that AutoIt don't go the way of the most software and become bigger and bigger to fit all user asked functions.

AutoIt gives me the possibility to write a small programm within minutes and (with the Gui) interact with the user. And also the possibility to feed my batchfiles with the users input. I don't have to start a 13Megs or more big IDE.

Ok, I'm running out of TOPIC... :D

If a script is to be used on more than one computer, you would have to FileInstall the exe to use it.

I think, that's not the problem. I have to install also the AutoIt script or the compiled exe. Wy not put an extra exe of 35K in the same directory. Other people put tons of dll in their installation dirs.

In some cases, you may not have write access to the directory where the script is running from or may not be allowed to dump files elsewhere on the system, so you would have to exit with a failure.

sort.exe -n < unsorted.txt > %tmp%\sorted.txt

No problem. This takes the output to the client.

Sorry for OT. I'm running in a principle discussion.

Pekster cu

arctor

Link to comment
Share on other sites

One small point I'd like to make... running an external utility is often slower than a home-brewed function simply because:

start the application + passing it the data + allowing it to interpret + getting the data back > overhead caused by AutoIt's internal workings.

Link to comment
Share on other sites

Hi Valik

...start the application + passing it the data + allowing it to interpret + getting the data back...

I agree with that.

But it is similar, as the question: What is faster? A Script or a compiled program? It depends on the job and how large it is.

The interpreter opens the file, reads the first line and starts, no matter how large the script is (DOS Batch). A compiled program has to be loaded complete into memory, before it starts to work. But the running process ist faster.

For example in case of asking a database the function inside the same programm will be faster, because the job (asking the database) is very small.

But doing a numeric sorting on a huge file (big job) maybe will be faster with a extra application which is written for that job. In this case the time for passing instructions to the application and getting the data back is not the main time killer.

arctor

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...