Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

How To Sort Numbers In Order?

Is there a built in function to do this?

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Does Sort Order Change Using The MSFlexGrid's Sort With Different Regional Settings?
I have an MSFlexGrid in our product that sorts based on two columns (using a string sort). One of our clients, who is using Windows 2000 with the Finnish Regional Setting appears to be getting a slightly different sort order. Does the MSFlexGrid use the windows Regional Setting to determine the sort order? If so, is there any way I can override this? I'd rather not use the compare event because of the efficiency (it already takes a very long time, I don't want it to take a very long time times 10!).

If the sort order does not change with different Regional Settings, then is there any other way that we could be getting different orders?

Thank you very much!


2 Listboxes - How To Sort One Listbox Based On Sort Order Of Other?
I have 2 listboxes..
one contains numbers, the other strings.. the numbers correspond to the string in the other listbox.

how can i sort listbox1 (the numbers) using sorted = true, and be able to sort listbox2 according to how listbox1 was sorted, so each number in listbox1 still corresponds to the correct string in listbox2?

Random Numbers--same Numbers/different Order
HI, I'd like to display 4 numbers randomly. I would like the same 4 numbers to be displayed each time the random code runs but in a different order each time.

I'v etried the ran() function but it doesn't quite do the job.

Any ideas?

Order & Sort (sql-ado) =
I'm using VB6, Access97 on WinXP

This is my SQL code...

VB Code:
ReptData.Open "SELECT * FROM Accounts ORDER BY Type", db'data sent to interface (user)ReptData.Close

I get this for data... (FYI.... "bla, bla" is field AccName)

HEADING 1 (Type)
hhhhhhhhhhhhhhhhhhh (bla, bla)
ccccccccccccc (bla, bla)
wwwwwwwwwwwwww (bla, bla)

HEADING 2 (Type)
kkkkkkkkkkkkkkkkkkk (bla, bla)
aaaaaaaaaaaaa (bla, bla)
ppppppppppppppppppppppppp (bla, bla)

Everything is fine except that the "bla, bla" data is not sorted correctly. In Access 97 you can sort two (or more)columns.

I want to be able to do the same in vb6.

SHOULD BE>.......

HEADING 1 (Type)
ccccccccccccc (bla, bla)
hhhhhhhhhhhhhhhhhhh (bla, bla)
wwwwwwwwwwwwww (bla, bla)

HEADING 2 (Type)
aaaaaaaaaaaaa (bla, bla)
kkkkkkkkkkkkkkkkkkk (bla, bla)
ppppppppppppppppppppppppp (bla, bla)

Any suggestions? How can I sort by Type and AccName


SQL Sort Order.
(I initially had this on the SQL forumns but they are telling me I need to post here as it's SQL through VB6)

I'm running the following query through the visual basic 6 data designer against a SQL 2005 database.

SELECT `Part No`, SUM(Val - `Cost Of Sale`) AS Margin, Description,
    `Product Group`
FROM tblSales
WHERE (`Invoice Date` BETWEEN (# 01 / 01 / 2006 #) AND
    (# 01 / 02 / 2007 #))
GROUP BY `Part No`, Description, `Product Group`
ORDER BY SUM(Val - `Cost Of Sale`) DESC

This works OK apart from the sort order.  It lists margins of "0" first, then "- numbers" and then the order is correct, i.e. largest positive number followed by the next largest and so on.  

i.e. 0, 0, 0, -14, 590, 570, 300, 200, etc.

I want to be able to display so that largest positive number followed by the next largest number are at the top, eventually going to 0 and then eventuially listing the - numbers.

i.e. 590, 570, 300, 200, 0, 0, 0, -14.

I'm presuming this isn't happening now because maybe the margin field isn't numeric?  

I've tried to use a CONVERT statement but get errors.  Is there another way to convert the calculated margin field to numeric so I can order the column descending?


Sort Without Using Order BY ?
How do you access the data in reverse order. I do not want to use Order By on a coluum. I want to get the last record out first and so on. My select statement is as follows.

Code:strSQL = "select * from DiscussionTopicComment Where ComboHex = '" & GetRecordNumber & "'"

Where would I use DESC? Or is this even possible?


Different Sort Order In VB And ADO !?

I experience problems with sort orders.
If i execute "SELECT & FROM table ORDER BY field1 ASC" and put it in a recordset variable and create an array by doing arr=rstemp.getrows

then i should have the sorted array in arr, right?

So far so good.
Then i start searching in this array for items, using the binary search algoritm (Thanx 2 Cimperiali)... but then i see a big problem!

for example the values "BIG001" and "BIG???"

In ADO "BIG001">"BIG???" is true!
in VB "BIG???">"BIG001" is true!

As you can see, this will give problems for the algoritm because it needs to be a sorted array according to vb!

Please help me out here!


Sort Keys & Order

I have a small database on a worksheet, and I'm trying to give the user various sort options using a Combo Box on the worksheet. I have all of the sorts working, but I was thinking... the next time I do this (and I do this often), can I create one Public Sub to sort my data various ways and call that Sub using variables to change the way the data sorts?

Here's some code I have now:

Sub Sort_by_Employee()

Range("rngCompleteList").Sort _
Key1:=Range("A2"), Order1:=xlAscending, _
Key2:=Range("B2"), Order2:=xlAscending, _
Key3:=Range("C2"), Order3:=xlAscending, _

End Sub

Basically, I have that little block of code set up in different ways (due to Keys) in different Subs. What I want to do, is create variables (such as varKey1 for instance) and insert them where the Ranges and Orders are so I can have one Sort Sub instead of a whole lot, and then I'll just write a Select Case that changes the variables as needed.

Can this be done? Also, if possible, could someone post some sample code for me to look at as to how this gets done? I've been altering my code trying to get this to work, but I've been unsucessful and I sometimes think I'm very close...


Results Sort Order...
Hello All,

I would like to create a "smart" sort-order to my results, something
like "more likely" or "match level", for example, if the user search for
"word_A word_B" I would like to bring all the "titles" that contain partial
match with "word_A" or "word_B", (so far very easy to do), my problem is
the sorting...I would like the result to be sorted by the match level, at the
top I would like to show (if exist) the exact match, then "titles" that holds
both words, and then "titles" that holds only one of the words...

I hope that I made it clear...

Sort Variables Into Order
I have 6 numbers dimensioned as variants. I put each number onto a label. How could I sort it so that the lowest number goes onto the first label and the highest onto the 6th label? All the numbers inbetween would also be in order of the labels. Thanks in advance!

ListView Sort Order

In my application i am sorting ListView1 by ID but it is sorting in the way windows does (See Image Below)

Any ideas of how i can stop this happening?


Sort Listbox Order
i was wondering how i would display data in the list in numerical order, the largest being at the top

Order (sort) A Column
I have a listview with 3 columns.
First I fill the listview with a script, after the script I want it to re-order automaticly. (so it must be done after the script)

How do I re-order the listview with the value's of column 3??

Without clicking the column header!!

Sort Order Of Treeview
I have a treeview control that lists all the servers, and when drilled down, lists all the subfolders. The problem is, when the folders are listed they are not in any particular order.

How do I get the folders to list in alphabetical order in a treeview control?


Sort Order Question
I have 2 sets of data for example:

label sort_order
------------- ---------------
name 3
phone 1
address 2

There are actually 16 pairs. I believe this is probably best done with an
array but I will yield to experience.

What I want to do is end up with an SQL select statement like

select phone,address,name from ....

built according to the sort order.

I thought of creating a temp table but thought an array would be easiest
because I should be able to loop through it.

Example code is always welcome.


Filesystem Sort Order
I have a program that reads image files in a folder and converts them to pdf files.  Does anyone know a way to control the sort order by date and time or by name of the filesystem object in the -For each file in files collection.  My code below:

'loop through tif files
Set fld = fso.GetFolder(sscantemp)
Set tFils = fld.Files
For Each tfil In tFils

I need to convert the files in some known order because I am combining them into one pdf file along with previously converted files.

Sort Order By VB Code
In order to sort order in Access Data View, I selected 3 adjcent columns as the same time and then click "sort ascending" button from "Table Data Sheet Toolbar". Is it possible to do the same thing in VB6 code using SQL statement or DAO syntax (or even something else)?

Set Sort Order At Runtime
Back to the slow forum.

I design my reports in CR8 and call them in my VB6 app using CrystalReport control.

I use the .SelectionFormula property to pass custom record selection criteria.

I need to pass custom sort order criteria for some reports at runtime.

I also want to make a stab at setting custom group order criteria at runtime.

Any help?

Experience is a bad teacher for its exams precede its lessons.

Edited by - GoodGuy on 7/13/2007 7:44:11 AM

Sort Strings In Certain Order
I have an array of AlphaNumeric Strings that i want to sort in a specific order.
For example, if I have 4 strings like the following...

I want them to sort in the order of...

They will always start with a number and the smaller the number, the closer to beginning of list. Any ideas on how to do this in VB?

Sort An Array In Alphabetical Order
Hi i was wondering about sorting an Array in alphabetical order.
Is this posible?
eg data

people(1) = Me
people(2) = You
people(3) = Dad
people(4) = Harry
people(5) = John


Changing Sort Order On Combo Box
I need to sort a Combo Box list in descending order. Currently, the list is sorted, but the lowest(and oldest) values are shown first. Over time, the list has grown to several thousand entries, but only the most recent values are worked on frequently.

Is there a way to change the sort order, perhaps through an API call? I could sort the values myself before plopping them into the Combo Box, but it would take some significant code changes.

Adodc1 Data Sort Order
I am using an adodc1 object that is used via a dbcombo box to autopopulate. If I INSERT an entry into the database and use the command

The new data is listed but the items in the DBCOMBO box are not sorted into any order. How can I force the adodc object to sort in an ascending order?

ORDER BY To Sort Strings And Numerics
I have a field 'ProjectNo' that has both strings and numerics

This is the solution I came up with to sort the strings as strings and numerics as numerics. Specifically I'm concerned with the ORDER BY. It works but has increased the execution time significantly.

What can I do to make the order by more efficient?


SELECT PROJECT.ProjectNo ,PROJECT.RevisionNo, CustomerName ,logbookcustomer, PROJECT.Application , ModelNoA & ',' & ModelNoB & ',' & ModelNoC as [ModelNo] ,ApprovalStatus, Customer.CustomerNo as [CustomerNo], Users.LName & ', ' & Users.FName as [EngineerName] ,ProjectComments
FROM ((Customer RIGHT JOIN Project ON Customer.CustomerNo = Project.CustomerNo) LEFT JOIN Users ON Project.EngineerNo = Users.EngineerNo)
iif(IsNumeric(PROJECT.ProjectNo),iif(len(PROJECT.ProjectNo) > 4, Val(PROJECT.ProjectNo), '0' & PROJECT.ProjectNo),PROJECT.ProjectNo) DESC

Wrong Sort Order When Grouping
Are there any swedish users on this forum using Crystal reports?

I think I have found a bug and want to confirm this.
If I group on a field containg following data:

bägare, börs, bil, bur

the order becomes:


but it shoild be


Crystal thinks 'å' and 'ä' is 'a' and 'ö' is 'o'

On the other hand if I have following data

ägare, boll, öl

the order becomes (correctly)


So Crystal can order swedish characters if they are the first one in a word but not as second or later.

Does this makes any sense at all?

Changing Sort Order In Listview
I have a listview and some data being shown. I would like to allow the user to change the sort order by clicking on the columns.

Is there any way to do that? A lot of applications do so i guess i could do that.

Thanks in advance.

Help Treeview Reverse Sort Order
How can you make a treeview reverse sort order

I have a treeview that has years in it and an alphabetical listing of names.

I want it to have the alphabetical names at top and then the years.


then then years

When I do sort = true it put the alphabetical part at the bottom and the years like

Sort Order By Check A List Box
Hi vbf:

I have a list box with the check box option active, and I want to sort order the items alphabetically and by check box activated, like the References menu into VB application, or just show the items that has the check box activated.

Thanks in advance and regards.

**Resoolved** How To Sort In Assending Order > Thanks All
Hi guys.
feeling rearly thick having to ask this but i cant find away and this is rearly puzzling.

I have a access97 database that my vb program reads.
i am using jet database

my database consists of 4 colums.
Chart Rank, Artist, Title, Year

In the database they are all in the order of Chart Rank (1-40)

but when i run my program most are not displayed in order.

I have to convert the database to access 97 from 2000
I have re-sorted and re-sorted the database in asscending order but this still does not work.

If i view the 97 database on its own for the first time everything is in order.
But once i run my vb program it is displayed out of order, if i then close the program and view the 97 database again, it is this time also out of order.

I need a way so that users can sort the database by either year or chart rank in assending/desending order.

Private Sub Command11_Click()

MyQuery = whichcolum.Text & " " & LIKE & " '" & "*" + searchinput.Text + "*" & "'"
Data1.RecordSource = "SELECT * FROM records WHERE " & MyQuery
Data1.Recordset.MoveLast: Data1.Recordset.MoveFirst
Text9.Text = " Your search has produced " & Data1.Recordset.RecordCount & " results"

End Sub

please could someone please please help me with some code that will use this sort or order method.

or even tell me what i am doing wrong in access.

Thanks too all in advance

Need Your Help With Bubble Sort And Alphabetical Order!!
I am in a computer programming class and the whole class is stumped on how to make our array of text boxes in alphabetical order.

Private Sub CmdSort_Click()
For j = 1 To 14
For i = 14 To 1 Step -1
If Asc(txttext(i).Text) < Asc(txttext(i - 1).Text) Then
StrHold = txttext(i).Text
txttext(i).Text = txttext(i - 1).Text
txttext(i - 1).Text = StrHold
End If
Next i
Next j
End Sub

This is what i have so far, but it only sorts by the first letters, and if we have 2 words with the same first letter it doesnt work. We have 15 text boxes that are all arrays, and we need to be able to type in anything in each box and then when we click the command button, they all have to be in alphabetical order. By alphabetical order i mean txttext(0) is the first one you would see in a dictionary, and txttext(14) is the last one you would see in a dictionary.

Can you help please??

Reverse Sort Order In A Combo Box? Please Help (vb6)
Hi Guys,
I am trying to list dates in a combo box.
the dates are added, year / month / day like this..

2005 10 04
2005 10 06
2005 10 05

the combo box with (Sort = true) will display them as..

2005 10 04
2005 10 05
2005 10 06

This is all well and good, except the recent dates are added the the bottom of the list, which could get excessive.
I would like to reverse the order so the most recent date comes to the top. Any help would be appreciated greatly.


ADO Recordset: See New Sort Order After Insert
My recordset is retrieved with:
 rs.Source = "select * from table order by lastname;"
A new record is added with:
 rs!lastname = "Smith"
This new record is appended to the end of the recordset.  I want it to be inserted into the correct recordset location based on the sort order of lastname.  (Note: lastname is indexed (duplicates OK) in my .MDB database).

Here's what I tried:
 rs!lastname = "Smith"
 varMark = rs.Bookmark
 rs.Source = "select * from table order by lastname;"
 rs.Bookmark = varMark  {ERROR! Invalid Bookmark!}

Unfortunately, by closing and reopening my recordset, the bookmark is invalidated -sigh....  What is the correct way to do this?

Sort Listbox In Alphabetic Order.
I guess i could code this myself butn before i do Í just want to check if someone has done this allready.

I filla listbox from a txt file with the open and input command.... So i guess the best way todo it is before adding it to the listbox?


Query Using Order And Sort Property

Can i ask help from you guys once again.
I have a query which sums the field and get its result using DAO.

sqlshare = "Select sum (FCREDIT-FDEBIT) from MSHR_SL where FMEMNR = '" & txtFMemnr.Text & "'"

What i want to happen is to sum all the records in the field and get the last date of transaction using DAO.
In my database i have 3 fields,

Debit     Credit    Date
15.00        2.00        10-12-2005
20.00        1.00        11-25-2005

RESULT : P33.00 as of 11-25-2005

Hope you can help me PLEASE.

Edited by - Jam_10 on 1/26/2005 12:04:56 AM

Ascending/sort Order In Excel
 I need help in arranging the columns in ascending order.

Assuming i got 10 columns and 10 rows.

And i want to arrange in ascending order based on column "C". i,e when we arrange based on col "C" all the other columns in that row also arrange the same way as column "C".


Edited by - chand22 on 5/12/2003 2:22:06 PM

I Want To Sort From 10 Values(Descending Order)
 guys, i want to sort (Descending order) of 10 values.

Can anyone help me on this.

Reverse Sort Order For Comboboxes?
Is there an easy way to sort in REVERSE order a Combo Box?

Nicolas LeBlanc
Software Engineer
Ordiplan Inc.

How Can I Set The Sort Order Of A ListView Control?
i want to set the sort order of a listview control like:
1. @
2. +
3. letters a...z
4. symbols {,},[,].. etc

any ideas???

User-friendly Way To Customize Sort Order
I have a very large lookup table in Access.

I have added an "Order" column where the user can assign each record a number so that the table can be sorted in the order the user wants rather than the order the items were entered.

The lists are getting rather large and hand entering the "Order" value is now a major pain.

Does anyone know where I can get a drag and drop ordering form/control similar to the Access Tab Order wizard? One where I can just select a record (or group of records), drag it to the desired row in the table and drop it. Then the code automatically changes the order column values.

Has anyone built one of these before? Would you be willing to share?


Export To CSV - Column Sort Order Problem

I'm attempting to export a table from Excel and sorting the first column (type Long) Ascending (0-900). It appears sorted on the screen, but it ALWAYS exports sorted Ascending based on the second field (type Text). This is a major headache, and I can't see a way of setting the column sort order in the exporter.

How can I export, sorted on the first column??

Best regards,

Edit by Moderator:
Please post Excel questions, in the Excel forum.

Thank you.

Internet Explorer Link Sort Order.
I have started to plan a new Favorites links program. But after manually sorted and moved links in the directory, the order of the links in Internet Explorer has changed.

You can manually drag the links/folders in the order you want.

Where does IE keep this sort order? And can I read it , and change it?


List View Sort In Date Order
Hi everyone, I'm stumped... maybe someone has an answer in this forum.
I have a listview that has 3 columns, one of them is just a simple description, the other is a numeric name, and the third is a date. I need to be able to sort in descending order when clicking on the header of each column. The description field is no problem, I just use listview.sort. The problem is when I try to sort dates, if I use the same thing I get 8/01/01 before 12/01/01 because 8 comes after 1 alphabetically... is there anyway to do this? I can only think of clearing the listview, sort the dates and then load them to the listview again, but that would take too long... and it seems like going the long way about it.
Please help!


How Can I Sort Array Elements In Ascending Order?
How's everyone doing?

I just wanted to know if anyone can tell me how I can sort the elements of an array in ascending order. Thanks

How To Sort 4 Columns Of Data In Ascending Order?
Name1 Volume1 Name2 Volume2
A 18 B 100
B 59 C 43
D 356 C 248
E 160 B 20
A 215 D 133

How do i sort Volume1 and Volume2 in ascending order?
While sorting Volume1, it should include the values in volume2.
So while sorting Volume1, the alphabets in the name column should also change accordingly.

So the end product should look like this:
Name1 Volume1 Name2 Volume2
A 18 D 133
B 20 E 160
C 43 A 215
B 59 C 248
B 100 D 356

How do i write the code using Visual Basic to sort the data accordingly?
Note: i have 65536 rows in Name1,Volume1 and 65536 rows in Name2,Volume2
Can anyone help? Thanks.

How To Change Address Book Sort Order ?
Hello everybody,

I'm totaly neewbie with interop component in C# but my boss ask to me to develop an application to change address book sort order.

I've found this article:
but I don't understand how to use this under C# ?

In more details, each user has this personnal address book and there is a company address book too.
When the user create an email we want that outlook resolve fisrt in company address book before personnal address book (depend on user configuration)

It ask to me: (see attachment)
- To add the company list
- To put the compagny group in show this address list first

Can you help me ?

Thx a lot

Refresh Data On Picking Sort Order
I have an .asp page displaying a recordset say col1,col2,col3,col4 from a table.Also there are 4 radio buttons col1,col2,col3,col4
On click of any of the radio buttons I would like the rs to be sorted by the corresponding column

say if the clicks on the col4 radio button, the page needs to be refreshed with the rs based on
"select * from tab1 order by col4"

I know this must be simple. Could anyone help?

Sort An Access Database In Inverse Order
My Database is already created with data already typed in. The first column is a numerique column which represents a certain Company ID.
I want to sort that column in order (20 to 1 not 1 to 20). Once that is done, there is a button in my program that adds data to this database, I want to check the last number created in the column and create the number after that.

If my highest number is 20 then I want the button to create 21


How To Set The Numbers By Order
can you set the numbers by order?


then b(1)= a(1),b(2)=a(3),b(3)=a(5),b(4)=a(2),b(5)=a(4)

Order Numbers

I need to sort numbers from my database MSAccess trought SQL but the problem it was the order of the numbers.
The datatype of the field,to extract the numbers, it was "Text"

This is that I have:


This is that I want:


and so on

and I had put this in my SQL code:

VB Code:
SQL = "Select * from tabelaseccao "If Index = 1 Then SQL = SQL & " ORDER BY CAST(classe as INT)"

but the code don't work...the error is:

run time error '3075'
Syntax error(missing operator),in query expression 'CAST(classe as INT)'

Any help please?


Critique My Solution To Finding The Sort Order Of An Array
THE PROBLEM: An array of txtBoxes can be dragged and dropped and when the submit button is hit, their order from top to bottom must be logged. The form resizes proportionally, so we can't check spots, we need to figure out the sort order of each

MY SOLUTION: An invisible sorted list box, format the txtBox tops to "00000" with added sixth digit containing the value of the variable of the array. Take the last digit of each item in the list box and put it into the DragLog.

Here's the bit code I tested this idea out with:

Option Explicit
Dim ArrayDepth As Integer
Dim StrArray(8) As Integer
Dim x As Byte
Dim DragLog As String

Private Sub Command1_Click()
Randomize Timer
Erase StrArray()
ArrayDepth = Int(Rnd * 7) + 2 'from 2 to 8 txtBoxes to sort
For x = 1 To ArrayDepth 'create hypothetical array
StrArray(x) = Int(Rnd * 13000) + 3000 'top min. 3000, top max. aprox. 16000

For x = 1 To ArrayDepth 'fill list with array
List1.AddItem Format(StrArray(x), "00000") & x 'adding x as sixth digit
Next 'identifies w/o changing order

DragLog = vbNullString 'convert sorted list to Log
For x = 0 To ArrayDepth - 1
DragLog = DragLog & Right$(List1.List(x), 1)
Text1.Text = DragLog
End Sub

MY QUESTION: Okay, it definately works, but what I really want to know is, if you joined a project and came across this bit of code, what would you think? Is this a goofy way of doing things and could you think of a more straightforward and acceptable way of finding out the sort order of this array?

Copyright © 2005-08, All rights reserved