Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Faster Array Sort

I am "sort challenged" I guess because I can't seem to find a better way to sort this array than below.  Any help?

    For i = 0 To UBound(SeriesRslts, 2) - 2
        For j = i + 1 To UBound(SeriesRslts, 2) - 1
            If CSng(SeriesRslts(14, i)) < CSng(SeriesRslts(14, j)) Then
                For k = 0 To 14
                    SortArr(k) = SeriesRslts(k, i)
                    SeriesRslts(k, i) = SeriesRslts(k, j)
                    SeriesRslts(k, j) = SortArr(k)
                Next
            End If
        Next
    Next

Open in new window

0
Bob Schneider
Asked:
Bob Schneider
  • 6
  • 6
  • 5
8 Solutions
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
where is the data coming from?

can you give a sample of what the data is?
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
it would be helpful to see what the format of the data is in now and what you want it in.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
you may also want to have a look at this SUB from 4Guys, which will allow you to sort by either dimension

http://www.4guysfromrolla.com/webtech/011601-1.shtml

or this one

http://www.4guysfromrolla.com/webtech/012799-3.shtml
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Scott Fell, EE MVEDeveloperCommented:
BobbaBuoy, when you are doing this via asp/vb, you can only get so fast.  I would look to see if this is something you can do via javascript/jquery or by a sql statement.  I would encourage you to post a question using only the mssql server tags.  Not that either of us couldn't help you, but there are some very good sql guys that only seem to answer sql questions when that is the only topic.  

If you want to stick with asp, I wonder if a dictionary object would be better http://support.microsoft.com/kb/246067

Otherwise, this type of sorting is slow.
http://www.4guysfromrolla.com/webtech/011001-1.shtml
http://www.4guysfromrolla.com/webtech/011601-1.shtml
0
 
Scott Fell, EE MVEDeveloperCommented:
On the JS side, if this is going to a table, I have had good success with datatables http://www.datatables.net/ Also there is http://listjs.com/examples, http://tablesorter.com/docs/ and http://tableclothjs.com/ to start.

These will be much faster than server side.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
agree with both of Scotts points, although I don't think the two links from 4Guys would be slower than what the original code. Ideally, if the data comes from a database, that would be the best place to do the sorting, otherwise you indeed will get slow sorting doing this on the server side.
0
 
Bob SchneiderAuthor Commented:
I will begin looking into all of these suggestions.  The data is sorted by a single data type.  A sample can be found here: http://www.gopherstateevents.com/series/series_results.asp?series_id=19&year=2014 

This example is very small so it works fine but if you select the "Central MInnesota" series it will take considerably longer and if you choose the "Lake Series" it will be minutes before it generates the results.

I appreciate the suggestions and will keep plugging on this.

BTW, the data that populates the array comes from a race results database.  I will post that code for consideration:

1) All series participants are written to a table.  The first recordset gets the dimension for the array to avoid redim preserve.  The second recordset gets their data.  There are a maximum of 10 races in a series so I start by including 10 places for points.  Some races have not happened yet.
    'get total by gender and dim array to avoid redim preserve
    Set rs = Server.CreateObject("ADODB.Recordset")
    If iAgeTo = 0 Then
        sql = "SELECT ParticipantID FROM SeriesParts WHERE SeriesID = " & lSeriesID & " AND Gender = '" & sGender & "'"
    Else
        sql = "SELECT ParticipantID FROM SeriesParts WHERE SeriesID = " & lSeriesID & " AND Gender = '" & sGender & "' AND Age >= " & iAgeFrom
        sql = sql & " AND Age <= " & iAgeTo
    End If
    rs.Open sql, conn, 1, 2
    If rs.RecordCount > 0 Then ReDim SeriesRslts(14, rs.RecordCount)
    rs.Close
    Set rs = Nothing

    i = 0
    Set rs = Server.CreateObject("ADODB.Recordset")
    If iAgeTo = 0 Then
        sql = "SELECT ParticipantID, PartName, Age, Gender FROM SeriesParts WHERE SeriesID = " & lSeriesID & " AND Gender = '" & sGender 
        sql = sql & "' ORDER BY PartName"
    Else
        sql = "SELECT ParticipantID, PartName, Age, Gender FROM SeriesParts WHERE SeriesID = " & lSeriesID & " AND Gender = '" & sGender & "' AND Age >= " 
        sql = sql & iAgeFrom & " AND Age <= " & iAgeTo & " ORDER BY PartName"
    End If
    rs.Open sql, conn, 1, 2
    Do While Not rs.EOF
        SeriesRslts(0, i) = rs(0).Value
        SeriesRslts(1, i) = Replace(rs(1).Value, "''", "'")
        SeriesRslts(2, i) = rs(2).Value
        SeriesRslts(3, i) = rs(3).Value
        For j = 4 To 14
            SeriesRslts(j, i) = "0"
        Next
        i = i + 1
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing

Open in new window


Once I have the participants I go through the races in the series and assign them points based on their place (either overall or within their age group):
    For i = 0 To UBound(SeriesRslts, 2) - 1
        For j = 0 To UBound(SeriesRaces, 2) - 1
            If CDate(SeriesRaces(2, j)) > Date Then
                SeriesRslts(j + 4, i) = "0"
            Else
                SeriesRslts(j + 4, i) = GetMyPts(SeriesRaces(0, j), SeriesRslts(0, i), SeriesRslts(2, i))
            End If
        Next
    Next

Private Function GetMyPts(lThisRaceID, lThisPartID, iMyAge)
    Dim iMyPl
    Dim bInRace
    Dim iNumFin

    iNumFin = 0
    GetMyPts = 0
    iMyPl = 0
    bINRace = False

    Set rs = Server.CreateObject("ADODB.Recordset")
    If CInt(iAgeTo) = 0 Then   'get open standings
        sql = "SELECT ir.ParticipantID FROM IndResults ir INNER JOIN Participant p ON ir.ParticipantID = p.ParticipantID WHERE ir.RaceID = " 
        sql = sql & lThisRaceID & " AND p.Gender = '" & sGender & "' AND ir.FnlTime IS NOT NULL AND ir.FnlTime <> '00:00:00.000' ORDER BY ir.EventPl" 
    Else
        sql = "SELECT ir.ParticipantID FROM IndResults ir INNER JOIN Participant p ON ir.ParticipantID = p.ParticipantID "
        sql = sql & "INNER JOIN PartRace pr ON ir.RaceID = pr.RaceID WHERE pr.RaceID = " & lThisRaceID & " AND ir.RaceID = " 
        sql = sql & lThisRaceID & " AND ir.FnlTime IS NOT NULL AND ir.FnlTime <> '00:00:00.000' AND p.Gender = '" & sGender & "' AND pr.Age >= " 
        sql = sql & iAgeFrom & " AND pr.Age <= " & iAgeTo & " ORDER BY ir.EventPl"
    End If

    rs.Open sql, conn, 1, 2
    If rs.RecordCount > 0 Then iNumFin = rs.RecordCount
    Do While Not rs.EOF
        iMyPl = CInt(iMyPl) + 1
        If CLng(lThisPartID) = CLng(rs(0).Value) Then 
            bInRace = True
            Exit Do
        End If
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing

    If CInt(iNumFin) > 0 Then 
        If bInRace = True Then GetMyPts = Round(((CInt(iNumFin) - CInt(iMyPl) + 1)/CInt(iNumFin))*100, 2)
    End If
[code]

Finally I add up their points and then do the sort:
[code]

    For i = 0 To UBound(SeriesRslts, 2) - 1
        For j = 4 To 13
            If SeriesRslts(j, i) = "0" Then Exit For
            SeriesRslts(14, i) = CSng(SeriesRslts(14, i)) + CSng(SeriesRslts(j, i))
        Next
    Next

Open in new window


Any suggestions on the process would be much appreciated.  From a sql server standpoint, I am not a trained db admin and do almost everytihing in asp code and from within the management console.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
if the data is coming from the database, then definitely use the database to handle the sorting. if there are millions or even hundreds of thousands of records, consider using a view, either way, using the database will give you better performance over trying to sort the data yourself.

i don't see the sql code that populates the array, but all you would need to do is add an ORDER BY clause. for example. in this code:

If iAgeTo = 0 Then
        sql = "SELECT ParticipantID, PartName, Age, Gender FROM SeriesParts WHERE SeriesID = " & lSeriesID & " AND Gender = '" & sGender 
        sql = sql & "' ORDER BY PartName"
    Else
        sql = "SELECT ParticipantID, PartName, Age, Gender FROM SeriesParts WHERE SeriesID = " & lSeriesID & " AND Gender = '" & sGender & "' AND Age >= " 
        sql = sql & iAgeFrom & " AND Age <= " & iAgeTo & " ORDER BY PartName"
    End If
    rs.Open sql, conn, 1, 2

Open in new window


you have the oder by clause already set, just add that to the query that pulls out the data that populates the array.

one other thing i noticed, not a biggie, but if you're just retrieving data from the database and don't have to be concerned about recordset cursors, instead of using

rs.Open sql, conn, 1, 2

use

set rs = conn.Execute( sql )
0
 
Bob SchneiderAuthor Commented:
THanks.  I usually use rs.Open to avoid errors if the recordset is empty.  I will revisit that.

The problem with using the database to do the sorting is that I need to get and display data from each race and not all runners run each race.  Then they need to be added up and sorted by the sum.  Is this workable and, if so, can someone point me in that direction?
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
THanks.  I usually use rs.Open to avoid errors if the recordset is empty.  I will revisit that.

no problem, you can still do the typical check (below) to see if the rs is empty or not:

if not rs.BOF and not rs.EOF then     '-- recordset has data

Sounds like you may want to move all of the logic over to a stored procedure then. I had this problem way back when I was first starting off in web dev and before I knew how useful SP's could be, my page took about 30 seconds to load because of all of the database calls I made. Once I re-wrote it to using a SP, it loaded up in about 7-10 seconds :)

I would finalize the approach you want to take, then close out this question and open a new one, with sql server topics over ASP, and you should get a more sql-oriented experts than here
0
 
Bob SchneiderAuthor Commented:
Learned alot as usual.
0
 
Scott Fell, EE MVEDeveloperCommented:
For a table like this, try out the javascript thing.  Using data tables will allow people quickly search the results and sort as they please.  You can send the data unsorted to the screen. Even use ajax.

http://www.datatables.net/examples/data_sources/ajax.html
$(document).ready(function() {
    $('#example').dataTable( {
        "ajax": '../ajax/data/arrays.txt'
    } );
} );

Open in new window

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
 
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>

Open in new window

You just need to get the data in a seperate page like this
{
  "data": [
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "$320,800"
    ],
    [
      "Garrett Winters",
      "Accountant",
      "Tokyo",
      "8422",
      "2011/07/25",
      "$170,750"
    ],
    [
      "Ashton Cox",
      "Junior Technical Author",
      "San Francisco",
      "1562",
      "2009/01/12",
      "$86,000"
    ],
    [
      "Cedric Kelly",
      "Senior Javascript Developer",
      "Edinburgh",
      "6224",
      "2012/03/29",
      "$433,060"
    ],
    [
      "Airi Satou",
      "Accountant",
      "Tokyo",
      "5407",
      "2008/11/28",
      "$162,700"
    ],
    [
      "Brielle Williamson",
      "Integration Specialist",
      "New York",
      "4804",
      "2012/12/02",
      "$372,000"
    ],
    [
      "Herrod Chandler",
      "Sales Assistant",
      "San Francisco",
      "9608",
      "2012/08/06",
      "$137,500"
    ],
    [
      "Rhona Davidson",
      "Integration Specialist",
      "Tokyo",
      "6200",
      "2010/10/14",
      "$327,900"
    ],
    [
      "Colleen Hurst",
      "Javascript Developer",
      "San Francisco",
      "2360",
      "2009/09/15",
      "$205,500"
    ],
    [
      "Sonya Frost",
      "Software Engineer",
      "Edinburgh",
      "1667",
      "2008/12/13",
      "$103,600"
    ],
    [
      "Jena Gaines",
      "Office Manager",
      "London",
      "3814",
      "2008/12/19",
      "$90,560"
    ],
    [
      "Quinn Flynn",
      "Support Lead",
      "Edinburgh",
      "9497",
      "2013/03/03",
      "$342,000"
    ],
    [
      "Charde Marshall",
      "Regional Director",
      "San Francisco",
      "6741",
      "2008/10/16",
      "$470,600"
    ],
    [
      "Haley Kennedy",
      "Senior Marketing Designer",
      "London",
      "3597",
      "2012/12/18",
      "$313,500"
    ],
    [
      "Tatyana Fitzpatrick",
      "Regional Director",
      "London",
      "1965",
      "2010/03/17",
      "$385,750"
    ],
    [
      "Michael Silva",
      "Marketing Designer",
      "London",
      "1581",
      "2012/11/27",
      "$198,500"
    ],
    [
      "Paul Byrd",
      "Chief Financial Officer (CFO)",
      "New York",
      "3059",
      "2010/06/09",
      "$725,000"
    ],
    [
      "Gloria Little",
      "Systems Administrator",
      "New York",
      "1721",
      "2009/04/10",
      "$237,500"
    ],
    [
      "Bradley Greer",
      "Software Engineer",
      "London",
      "2558",
      "2012/10/13",
      "$132,000"
    ],
    [
      "Dai Rios",
      "Personnel Lead",
      "Edinburgh",
      "2290",
      "2012/09/26",
      "$217,500"
    ],
    [
      "Jenette Caldwell",
      "Development Lead",
      "New York",
      "1937",
      "2011/09/03",
      "$345,000"
    ],
    [
      "Yuri Berry",
      "Chief Marketing Officer (CMO)",
      "New York",
      "6154",
      "2009/06/25",
      "$675,000"
    ],
    [
      "Caesar Vance",
      "Pre-Sales Support",
      "New York",
      "8330",
      "2011/12/12",
      "$106,450"
    ],
    [
      "Doris Wilder",
      "Sales Assistant",
      "Sidney",
      "3023",
      "2010/09/20",
      "$85,600"
    ],
    [
      "Angelica Ramos",
      "Chief Executive Officer (CEO)",
      "London",
      "5797",
      "2009/10/09",
      "$1,200,000"
    ],
    [
      "Gavin Joyce",
      "Developer",
      "Edinburgh",
      "8822",
      "2010/12/22",
      "$92,575"
    ],
    [
      "Jennifer Chang",
      "Regional Director",
      "Singapore",
      "9239",
      "2010/11/14",
      "$357,650"
    ],
    [
      "Brenden Wagner",
      "Software Engineer",
      "San Francisco",
      "1314",
      "2011/06/07",
      "$206,850"
    ],
    [
      "Fiona Green",
      "Chief Operating Officer (COO)",
      "San Francisco",
      "2947",
      "2010/03/11",
      "$850,000"
    ],
    [
      "Shou Itou",
      "Regional Marketing",
      "Tokyo",
      "8899",
      "2011/08/14",
      "$163,000"
    ],
    [
      "Michelle House",
      "Integration Specialist",
      "Sidney",
      "2769",
      "2011/06/02",
      "$95,400"
    ],
    [
      "Suki Burks",
      "Developer",
      "London",
      "6832",
      "2009/10/22",
      "$114,500"
    ],
    [
      "Prescott Bartlett",
      "Technical Author",
      "London",
      "3606",
      "2011/05/07",
      "$145,000"
    ],
    [
      "Gavin Cortez",
      "Team Leader",
      "San Francisco",
      "2860",
      "2008/10/26",
      "$235,500"
    ],
    [
      "Martena Mccray",
      "Post-Sales support",
      "Edinburgh",
      "8240",
      "2011/03/09",
      "$324,050"
    ],
    [
      "Unity Butler",
      "Marketing Designer",
      "San Francisco",
      "5384",
      "2009/12/09",
      "$85,675"
    ],
    [
      "Howard Hatfield",
      "Office Manager",
      "San Francisco",
      "7031",
      "2008/12/16",
      "$164,500"
    ],
    [
      "Hope Fuentes",
      "Secretary",
      "San Francisco",
      "6318",
      "2010/02/12",
      "$109,850"
    ],
    [
      "Vivian Harrell",
      "Financial Controller",
      "San Francisco",
      "9422",
      "2009/02/14",
      "$452,500"
    ],
    [
      "Timothy Mooney",
      "Office Manager",
      "London",
      "7580",
      "2008/12/11",
      "$136,200"
    ],
    [
      "Jackson Bradshaw",
      "Director",
      "New York",
      "1042",
      "2008/09/26",
      "$645,750"
    ],
    [
      "Olivia Liang",
      "Support Engineer",
      "Singapore",
      "2120",
      "2011/02/03",
      "$234,500"
    ],
    [
      "Bruno Nash",
      "Software Engineer",
      "London",
      "6222",
      "2011/05/03",
      "$163,500"
    ],
    [
      "Sakura Yamamoto",
      "Support Engineer",
      "Tokyo",
      "9383",
      "2009/08/19",
      "$139,575"
    ],
    [
      "Thor Walton",
      "Developer",
      "New York",
      "8327",
      "2013/08/11",
      "$98,540"
    ],
    [
      "Finn Camacho",
      "Support Engineer",
      "San Francisco",
      "2927",
      "2009/07/07",
      "$87,500"
    ],
    [
      "Serge Baldwin",
      "Data Coordinator",
      "Singapore",
      "8352",
      "2012/04/09",
      "$138,575"
    ],
    [
      "Zenaida Frank",
      "Software Engineer",
      "New York",
      "7439",
      "2010/01/04",
      "$125,250"
    ],
    [
      "Zorita Serrano",
      "Software Engineer",
      "San Francisco",
      "4389",
      "2012/06/01",
      "$115,000"
    ],
    [
      "Jennifer Acosta",
      "Junior Javascript Developer",
      "Edinburgh",
      "3431",
      "2013/02/01",
      "$75,650"
    ],
    [
      "Cara Stevens",
      "Sales Assistant",
      "New York",
      "3990",
      "2011/12/06",
      "$145,600"
    ],
    [
      "Hermione Butler",
      "Regional Director",
      "London",
      "1016",
      "2011/03/21",
      "$356,250"
    ],
    [
      "Lael Greer",
      "Systems Administrator",
      "London",
      "6733",
      "2009/02/27",
      "$103,500"
    ],
    [
      "Jonas Alexander",
      "Developer",
      "San Francisco",
      "8196",
      "2010/07/14",
      "$86,500"
    ],
    [
      "Shad Decker",
      "Regional Director",
      "Edinburgh",
      "6373",
      "2008/11/13",
      "$183,000"
    ],
    [
      "Michael Bruce",
      "Javascript Developer",
      "Singapore",
      "5384",
      "2011/06/27",
      "$183,000"
    ],
    [
      "Donna Snider",
      "Customer Support",
      "New York",
      "4226",
      "2011/01/25",
      "$112,000"
    ]
  ]
}

Open in new window

I have somebody else using this and you can see what I did here to get this in asp http:Q_28469969.html#a40204456
0
 
Scott Fell, EE MVEDeveloperCommented:
Can't type fast enough... I know that was after you accepted the answer, but do check that out.  It runs very fast.  That sample is sending 50 records to the screen for now.  1000 would be fine but anything more I would use ajax to pluck just 1000 (or even a hundred) rows of data at time.
0
 
Bob SchneiderAuthor Commented:
Thanks Scott.  I will look at this now.
0
 
Bob SchneiderAuthor Commented:
Where do I put the data itself?  I see it in your last code snippet and I understand that would be the results of my query (array) but don't see how that is used to populate the table.
0
 
Scott Fell, EE MVEDeveloperCommented:
It does a jquery ajax call.  

This line   "ajax": '../ajax/data/arrays.txt' would be something like   "ajax": '/api/getresults.asp'

$(document).ready(function() {
    $('#example').dataTable( {
        "ajax": '/api/getresults.asp'
    } );
} );

Open in new window


We can set the ajax call to send a post to the getresults.asp page that you can use to filter.  For now, make something static to get it going.  

To see how this works, set up static data using the exact structure, coding and data as this sample http://www.datatables.net/examples/data_sources/ajax.html.   Then start backing in your own data.

The jquery ajax info is here http://api.jquery.com/jquery.ajax/
  $('#example').dataTable( {
        "ajax": '/api/getresults.asp?series=123&gender=male'
    } );

Open in new window

We can keep building on this in another question.  

On your other question, if you don't get a good answer in a day, just delete the question and start over.  But this time, do not add asp as a topic and only ask about an sql query that is faster than what you are doing.  I hope that makes sense. I think some of the good sql guys back away when they see asp code.
0
 
Bob SchneiderAuthor Commented:
Thank you very much for all of your help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now