Solved

Sorting Numbers and Finding Next Highest Number

Posted on 2004-08-03
6
3,839 Views
Last Modified: 2012-05-07
I am needed suggestions on how to accomplish the following task with numbers.

Step 1: Sort the numbers.
Step 2: Select one of the numbers in the list.
Step 3: Find the next highest number from the number selected in step 2.

For example, my app is returning a list of numbers, such as:

1
1177
1189
12
1237
1261
1377

Of course, in the example above, the list is not truly sorted as 12 should be after the 1 in the list.
0
Comment
Question by:abnc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11706229
Well, there are many sorting algorithms, the easiest I think is sustitution:
Just scan all elements of array (minus one) and compare with the next element, if greater, swap them. Scan again until no swap is made.
once ordered select a number in the list (array) and the next element will be next highest number.
 
0
 
LVL 14

Expert Comment

by:aelatik
ID: 11706333
How do you get the numbers, in an arrray ? Where do you want them to get sorted ? In a listbox ?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11706343
Start creating an array with numbers:

Dim myArray(10) as Integer
myArray(1) = 1
myArray(2) = 1177
'etcetera

Use a for ... next statement
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 28

Accepted Solution

by:
vinnyd79 earned 250 total points
ID: 11706458
If the numbers are in a Listbox you can set the sorted property to true and use Format to add 0's so they sort properly.

List1.AddItem Format("1", "000#")
List1.AddItem Format("1177", "000#")
List1.AddItem Format("12", "000#")
0
 
LVL 13

Expert Comment

by:AlbertaBeef
ID: 11706494
If this is a listbox you can pad the items in the listbox with spaces, then it sorts properly.

IE:  if you populate the listbox this way, with spaces, then it will be sorted.

   List1.AddItem "   1"
   List1.AddItem "1177"
   List1.AddItem "1189"
   List1.AddItem "  12"
   List1.AddItem "1237"
   List1.AddItem "1261"
   List1.AddItem "1377"

0
 
LVL 13

Expert Comment

by:AlbertaBeef
ID: 11706514
lol, posted mine after vinnyd79's -  his would work just as well, even better...
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question