Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 160
Default Sort in striing?

Hi,


I have a string variable, containing names seperated by a comma, like:

NameString = "Peter, Dan, Hans, Carl, Bruce" etc...

I would like the string to by sorted, by the names between the commas,
so my string end up like:

NameString = "Bruce, Carl, Dan, Hans, Peter" etc...

And to make things worse, the number of commas/names can be different
from time to time...

Can anyone help me acomplish this?


Thanks in advance,

CE

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Sort in striing?

On Thu, 22 Nov 2012 11:21:55 +0100, "Charlotte E." wrote:

Hi,


I have a string variable, containing names seperated by a comma, like:

NameString = "Peter, Dan, Hans, Carl, Bruce" etc...

I would like the string to by sorted, by the names between the commas,
so my string end up like:

NameString = "Bruce, Carl, Dan, Hans, Peter" etc...

And to make things worse, the number of commas/names can be different
from time to time...

Can anyone help me acomplish this?


Thanks in advance,

CE


There are lots of VBA sort routines out there. Here's one:

===========================
Option Explicit
Sub SortString()
Dim NameString As String
Dim v As Variant
NameString = "Peter, Dan, Hans, Carl, Bruce"
v = Split(NameString, ", ")
Quick_Sort v, LBound(v), UBound(v)

Debug.Print Join(v, ", ")
End Sub


'--------------------------------------------
Sub Quick_Sort(ByRef SortArray As Variant, ByVal First As Long, ByVal Last As Long)
Dim Low As Long, High As Long
Dim Temp As Variant, List_Separator As Variant
Low = First
High = Last
List_Separator = SortArray((First + Last) / 2)
Do
Do While (SortArray(Low) < List_Separator)
Low = Low + 1
Loop
Do While (SortArray(High) List_Separator)
High = High - 1
Loop
If (Low <= High) Then
Temp = SortArray(Low)
SortArray(Low) = SortArray(High)
SortArray(High) = Temp
Low = Low + 1
High = High - 1
End If
Loop While (Low <= High)
If (First < High) Then Quick_Sort SortArray, First, High
If (Low < Last) Then Quick_Sort SortArray, Low, Last
End Sub
=================================
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Sort in striing?

Charlotte E. laid this down on his screen :
Hi,


I have a string variable, containing names seperated by a comma, like:

NameString = "Peter, Dan, Hans, Carl, Bruce" etc...

I would like the string to by sorted, by the names between the commas, so my
string end up like:

NameString = "Bruce, Carl, Dan, Hans, Peter" etc...

And to make things worse, the number of commas/names can be different from
time to time...

Can anyone help me acomplish this?


=========================
Public Sub SortInString()
Dim S As String, S1, Swap As String
Dim i As Integer, j As Integer

S = "Peter, Dan, Hans, Carl, Bruce"
S1 = Split(Trim(S) & ",", ",")
S = ""
For i = 0 To UBound(S1) - 2
For j = i + 1 To UBound(S1) - 1
If S1(j) < S1(i) Then
Swap = S1(i)
S1(i) = S1(j)
S1(j) = Swap
End If
Next
S = S & S1(i) & ", "
Next
S = S & S1(i)
MsgBox S

End Sub
===========================

Bruno


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Sort in striing?

Bruno Campanini wrote:

Charlotte E. laid this down on his screen :
Hi,


I have a string variable, containing names seperated by a comma, like:

NameString = "Peter, Dan, Hans, Carl, Bruce" etc...

I would like the string to by sorted, by the names between the commas,
so my string end up like:

NameString = "Bruce, Carl, Dan, Hans, Peter" etc...

And to make things worse, the number of commas/names can be different
from time to time...

Can anyone help me acomplish this?


=========================
Public Sub SortInString()
Dim S As String, S1, Swap As String
Dim i As Integer, j As Integer

S = "Peter, Dan, Hans, Carl, Bruce"
S1 = Split(Trim(S) & ",", ",")
S = ""
For i = 0 To UBound(S1) - 2
For j = i + 1 To UBound(S1) - 1
If S1(j) < S1(i) Then
Swap = S1(i)
S1(i) = S1(j)
S1(j) = Swap
End If
Next
S = S & S1(i) & ", "
Next
S = S & S1(i)
MsgBox S

End Sub
===========================


I don't know what the purpose of adding a blank entry at the end of the
string is for, but it's not necessary to sort. Also, Swap is a keyword (see
below). Also also, the Join keyword makes it unnecessary to manually
recombine the array back into the string.

If you use this method, do this instead:

Public Function SortInString(S As String, _
Optional Delimiter As String = ", ")
Dim S1 As Variant
Dim i As Long, j As Long

S1 = Split(Trim(S), Delimiter)
For i = 0 To UBound(S1) - 1
For j = i + 1 To UBound(S1)
If S1(j) < S1(i) Then Swap S1(i), S1(j)
Next
Next
S = Join(S1, Delimiter)
SortInString = S
End Function

Use it like so:

sorted = SortInString("Peter, Dan, Hans, Carl, Bruce")
or
sorted = SortInString("Peter, Dan, Hans, Carl, Bruce", ", ")

However, the method Ron Rosenfeld posted (using Quicksort) is probably
faster.

--
Her attitude has changed from "Anything Goes" to "Nothing Gets By."
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Sort in striing?

Auric__ submitted this idea :

[...]

However, the method Ron Rosenfeld posted (using Quicksort) is probably
faster.


Yes, faster if you are using hundreds or thousands of elemnts in one
string.
But, is it the case of a string which resides in one Excel cell?

Don't waste your time...

Bruno




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Sort in striing?

On Thu, 22 Nov 2012 19:26:41 +0000 (UTC), "Auric__" wrote:

However, the method Ron Rosenfeld posted (using Quicksort) is probably
faster.


The Quicksort algorithm should be faster unless the list is already nearly sorted. With short lists, however, the speed difference is probably not significant compared with a Bubblesort.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 160
Default Sort in striing?

Thanks guys - got it working :-)

CE



Den 22.11.2012 11:21, Charlotte E. skrev:
Hi,


I have a string variable, containing names seperated by a comma, like:

NameString = "Peter, Dan, Hans, Carl, Bruce" etc...

I would like the string to by sorted, by the names between the commas,
so my string end up like:

NameString = "Bruce, Carl, Dan, Hans, Peter" etc...

And to make things worse, the number of commas/names can be different
from time to time...

Can anyone help me acomplish this?


Thanks in advance,

CE

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Sort in striing?

I'm going to say that I've done exhaustive tests with various sorting
solutions and found none faster than dumping the list into a temp wks
and using Excel's Sort function followed by dumping the result back
into an array for processing, or into a ListBox if appropriate.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Sort in striing?

On Fri, 23 Nov 2012 13:07:33 -0500, GS wrote:

I'm going to say that I've done exhaustive tests with various sorting
solutions and found none faster than dumping the list into a temp wks
and using Excel's Sort function followed by dumping the result back
into an array for processing, or into a ListBox if appropriate.


I agree. And for multicolumn sorts, that is my preference. But for single or two column sorts, the coding seems simpler (for me) to just do it in VBA.
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Sort in striing?

Ron Rosenfeld formulated on Friday :
On Fri, 23 Nov 2012 13:07:33 -0500, GS wrote:

I'm going to say that I've done exhaustive tests with various sorting
solutions and found none faster than dumping the list into a temp wks
and using Excel's Sort function followed by dumping the result back
into an array for processing, or into a ListBox if appropriate.


I agree. And for multicolumn sorts, that is my preference. But for single
or two column sorts, the coding seems simpler (for me) to just do it in VBA.


I agree.., as long as the lists are relatively short. I typically store
lists on a hidden sheet that has named ranges for each list. I usually
leave them sorted after adding/removing items so they're 'ready-to-use'
however I need/want. Different case, though, for working with list
items on the fly.<g

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating workbook with an alpha sort sheet and a numeric sort shee cjlatta Excel Discussion (Misc queries) 2 January 28th 09 12:00 AM
sort function for dates does not sort properly in Office 2007 Exc. Rosalie Excel Worksheet Functions 1 November 22nd 07 10:25 PM
Pls. reply Sort Data and copy to next coulmn when sort order chang shital shah Excel Programming 1 August 19th 05 02:51 PM
Excel sort by Fill Color by custom list sort Dash4Cash Excel Discussion (Misc queries) 2 July 29th 05 10:45 PM
sort (on part of) string - originally posted under Tricky Sort Tom Ogilvy Excel Programming 0 August 6th 04 02:42 AM


All times are GMT +1. The time now is 11:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"