By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

String Bubble Sort

P: n/a
Hello,

Can anyone help out with a bubble sort for strings using ASP? I have a text
file of words id like to sort and count.

Thanks in advance for any help.

Gram.
Jul 19 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Gram wrote:
Hello,

Can anyone help out with a bubble sort for strings using ASP? I have
a text file of words id like to sort and count.

Thanks in advance for any help.

Gram.


Here's a quick example:
dim ar, i , j, vTemp
ar=array(dog,cat,bird,monkey)
response.write "Before Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
for i=0 to ubound(ar)-1
for j = i+1 to ubound(ar)
if ar(j) <= ar(i) then
vTemp = ar(j)
ar(j) = ar(i)
ar(i) = vTemp
end if
next
next
response.write "After Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next

To sort numbers, change "if ar(j) <= ar(i) then" to "if cdbl(ar(j)) <=
cdbl(ar(i)) then", or cint, clng, etc., depending on what type of numbers
you are sorting.
HTH,
Bob Barrows
Jul 19 '05 #2

P: n/a
Thanks for that Bob,

You forgot the quotes around the words for the strings though, doesn't work
without them
ar=array("dog","cat","bird","monkey")

Thanks again Bob

Gram.

"Gram" <gr******@hotmail.com> wrote in message
news:MI*******************@news.indigo.ie...
Hello,

Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count.

Thanks in advance for any help.

Gram.

Jul 19 '05 #3

P: n/a
Tim
bubble sort is rubbish and slow.

with small numbers this might be fine, but a bit of research will find much
better alternatives.
for example...
http://www.cs.ubc.ca/spider/harrison...ting-demo.html

even if you dont use any of these, they are really interesting.
Tim
"Gram" <gr******@hotmail.com> wrote in message
news:MI*******************@news.indigo.ie...
Hello,

Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count.

Thanks in advance for any help.

Gram.

Jul 19 '05 #4

P: n/a
Thanks,

Can anyone point to a site which gives examples of other sorting methods
using ASP?

Gram.
Jul 19 '05 #5

P: n/a
Couple of techniques here for sorting a list of files, but could easily be
used in your scenario. http://www.aspfaq.com/2025

Sorting / counting is better handled by a database than an interpreted
scripting language, IMHO. Why don't you use a database?

"Gram" <gr******@hotmail.com> wrote in message
news:O5*******************@news.indigo.ie...
Thanks,

Can anyone point to a site which gives examples of other sorting methods
using ASP?

Gram.

Jul 19 '05 #6

P: n/a
"Gram" <gr******@hotmail.com> wrote in message
news:O5*******************@news.indigo.ie...
Thanks,

Can anyone point to a site which gives examples of other sorting methods using ASP?

Gram.

<script language="JavaScript" runat="SERVER">
var s = "Hello,\n\nCan anyone help out with a bubble sort for strings
using ASP? I have a text file of words id like to sort and
count.\n\nThanks in advance for any help.\n\nGram.";
var a = s.match(/\w+/g).sort();
Response.Write(a.length + " word(s):<br>");
for (var i=0;i<a.length;i++) Response.Write(a[i]+"<br>");
</script>

Loading the text from a file is left as an exercise for the reader. ;-p

HTH
-Chris
Jul 19 '05 #7

P: n/a
"Gram" wrote:

Can anyone help out with a bubble sort for strings using ASP?
I have a text file of words id like to sort and count.


Assuming you have used Scripting.FileSystemObject to read the file into a
string, and furthermore that your words are delimited by something, it
should be simple. Here's an example:

Given str = {the contents of the text file}, del = {your delimiter}

JScript:
a = str.split(del)
a.sort()

vbscript:
a = Split(str,del)
{implement your own sorting routine for an array}
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #8

P: n/a
"Aaron Bertrand - MVP" wrote:

Sorting / counting is better handled by a database than an
interpreted scripting language, IMHO. Why don't you use a
database?


While I'm mostly in agreement, there is no denying the need to occasionally
sort things that simply don't -- or can't -- reside in a database, such as a
file listing for a folder. And frankly, VBScript could stand another native
Function for something this fundamental. This whole task is a 1-liner in
JScript:

var a = str.split(delimiter).sort()

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #9

P: n/a
> While I'm mostly in agreement, there is no denying the need to
occasionally
sort things that simply don't -- or can't -- reside in a database, such as a file listing for a folder.


Oh sure, I'm not saying it's cool that this functionality doesn't exist.

But for the performance either way, I would say it's worth experimenting
with stuffing a file list, or even the contents of a file, into a database
and test the performance against whatever other kludges you can present in
VBScript. :-)

What kind of things can't reside in a database, even if only temporarily?
Jul 19 '05 #10

P: n/a
"Aaron Bertrand - MVP" wrote:

What kind of things can't reside in a database, even if only temporarily?


Objects come to mind. I frequently construct arrays of objects, and
occasionally even sort them according to one criterion or another.

Admittedly, these objects could usually be deconstructed and their
important/differentiating methods or properties stored somehow in a DB, but
that seems like a bit more work than just using the .sort method of the
array.

That aside, there's another thing to consider with respect to web scripts --
if the total set of items to be sorted is small enough to display** on a
page, there's probably more overhead cost in sorting via DB INSERT/SELECT
than by sorting in place. If it's a large set of content, then it is
probably coming from a DB anyway, so my ordering happens there.

**OK - "small enough to display" is a bit subjective, but I generally adhere
to the principal that more than 1-2 screens of scrolling through a list is a
waste of time for everyone, and is something to be avoided as often as is
reasonable.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #11

P: n/a
> Admittedly, these objects could usually be deconstructed and their
important/differentiating methods or properties stored somehow in a DB, but that seems like a bit more work than just using the .sort method of the
array.
I'm not sure how you would sort an object? I would think you could sort a
property (such as its name) but an object itself? Could you give a
meaningful example?
page, there's probably more overhead cost in sorting via DB INSERT/SELECT
than by sorting in place.


I've seen some pretty good performance in a local recordset (no actual
writing to the DB), as opposed to some of the looping/comparison sorts out
there.

A
Jul 19 '05 #12

P: n/a
"Aaron Bertrand - MVP" wrote:

I'm not sure how you would sort an object? I would think
you could sort a property (such as its name) but an object
itself? Could you give a meaningful example?
One real-world example that pops up in here occasionally is the "sort a 2D
array" problem. This immediately gives rise to two points: (1) sorting in 2D
has to be defined on a case-by-case basis, and (2) In JScript, there is no
such thing as a multi-dimensional array. You can, however, sort arrays of
arrays. In that case, each element of the outer array is an object. A
typical sorting algorithm looks like this:

a.sort(function(x,y) { return x[j]<y[j] ? -1 : 1 })

Extending that to generalized objects, you can do this:

a.sort(function(x,y) { return x.PropName<y.PropName ? -1 : 1 })
I've seen some pretty good performance in a local recordset
(no actual writing to the DB), as opposed to some of the
looping/comparison sorts out there.


That is certainly one possibility, but it carries the burden of
[create,populate,sort,extract,destroy], for the sole purpose of utilizing a
somewhat optimal sorting algorithm. My point was that a native sorting
Function could achieve the same level of sorting efficiency for arrays, just
as the .sort method does in JScript.

And why am I focusing on arrays at all? Consider the original post. Read
text file and sort contents. I cannot imagine a more streamlined approach
than Split() --> Sort(). Is there an equivalent to Split() when feeding a
recordset (sort of an inverse of .GetString())? If so, this could be a
useful interface for sorting.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #13

P: n/a
Not sure how you would do it in VBScript but in C++ you would define
comparison operators that know what criteria to use for which object is
"bigger". The criteria might involve multiple properties and unusual
algorithms but the objects would know what to do. That way the sort function
doesn't have to know anything about the objects themselves. If you had more
than one kind of object then you would define comparison operators for each
possible combination.

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
ms*****@ielearning.com
http://www.ielearning.com
714.637.9480 x17
"Aaron Bertrand - MVP" <aa***@TRASHaspfaq.com> wrote in message
news:eu**************@TK2MSFTNGP09.phx.gbl...
Admittedly, these objects could usually be deconstructed and their
important/differentiating methods or properties stored somehow in a DB,

but
that seems like a bit more work than just using the .sort method of the
array.


I'm not sure how you would sort an object? I would think you could sort a
property (such as its name) but an object itself? Could you give a
meaningful example?
page, there's probably more overhead cost in sorting via DB INSERT/SELECT than by sorting in place.


I've seen some pretty good performance in a local recordset (no actual
writing to the DB), as opposed to some of the looping/comparison sorts out
there.

A

Jul 19 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.