Toon posts:

[Alg / VBScript] Array sorteren op itemlengte *

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik heb een array en die array moet gesorteerd worden op de lengte van de waardes die erin zitten dus bv

arry(0) = "abc"
arry(1) = "a"
arry(3) = "gg"
arry(4) = "weee"

dan moet ik heb zo krijgen

arry(0) = "weee"
arry(1) = "abc"
arry(3) = "gg"
arry(4) = "a"

weet iemand hoe dit moet?

  • Jaspertje
  • Registratie: September 2001
  • Laatst online: 18-05 15:53

Jaspertje

Max & Milo.. lief

http://www.google.nl/sear...oe=UTF-8&q=asp+sort+array

Wat heb je al gedaan? Laat eens wat code zien... lees ff de FAQ

[ Voor 53% gewijzigd door Jaspertje op 16-06-2004 09:22 ]


Verwijderd

Topicstarter
ja ik heb ik google gezocht maar kon niks vinden over het sorteren op de lengte van de waarde die erin zit....

  • Jaspertje
  • Registratie: September 2001
  • Laatst online: 18-05 15:53

Jaspertje

Max & Milo.. lief

Wat ie normaal doet is kijken naar getallen, als 1 > 2 dan..

wat jij moet doen is len() overal omheen zetten..

  • GigaDave56
  • Registratie: Juni 2001
  • Laatst online: 14-12-2025
Typisch geval van Bubble sort... :7

Not so Giga One
> I'd sell my soul for you, babe
> For money to burn, for you
> I'd give you all and have none, babe
> Just to, just to, to have you here by me... [Scooter - Rebel yell]


Verwijderd

Topicstarter
Ik heb dit al:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Function SortByLen(ByVal array)
    Dim i
    Dim ThisValue, NextValue
    Dim ThisLen, NextLen

    For i = 0 To UBound(array) - 1
        ThisValue = array(i, 1)
        NextValue = array(i + 1, 1)
        ThisLen = array(i, 1)
        NextLen = array(i + 1, 1)

        If NextLen > ThisLen Then
            array(i, 1) = NextValue
            array(i + 1, 1) = ThisValue
        End If
    Next
    SortByLen = array
End Function

Dim Arry(5, 2)

Arry(0, 0) = "0"
Arry(0, 1) = "aaa"
Arry(0, 2) = "0"

Arry(1, 0) = "1"
Arry(1, 1) = "a"
Arry(1, 2) = "1"

Arry(2, 0) = "2"
Arry(2, 1) = "aa"
Arry(2, 2) = "2"

Arry(3, 0) = "3"
Arry(3, 1) = "aaaaaa"
Arry(3, 2) = "3"

Arry(4, 0) = "4"
Arry(4, 1) = "aaaa"
Arry(4, 2) = "4"

Arry(5, 0) = "5"
Arry(5, 1) = "aaa"
Arry(5, 2) = "5"

Dim NA : NA = SortByLen(Arry)

  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 07:58

gorgi_19

Kruimeltjes zijn weer op :9

Titleschange; 't is geen VB, maar VBScript en ASP heeft er niets mee te maken.

Digitaal onderwijsmateriaal, leermateriaal voor hbo


  • onkl
  • Registratie: Oktober 2002
  • Laatst online: 08:50
Additioneel, al over nagedacht wat te doen met twee gelijke lengtes? Op alfabet oid?
Evt. een dubbele sort: Eerst bubble sort op alfabet (of wat je ook wilt) en dan op lengte?

Verwijderd

Topicstarter
dubble maakt niet uit :)

heb nog wat geprobeerd: (komt wel dichter in de buurd maar werkt nog niet helemaal goed :(
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Function SortByLen(ByVal array)
    Dim i, x
    Dim iLen, xLen
    Dim i0, i1, i2
    Dim x0, x1, x2

    For i = 0 To UBound(array) - 1
        iLen = Len(array(i, 1))

        For x = i + 1 To UBound(array) - 1
            xLen = Len(array(x, 1))

            If xLen > iLen Then
                i0 = array(i, 0)
                i1 = array(i, 1)
                i2 = array(i, 2)

                x0 = array(x, 0)
                x1 = array(x, 1)
                x2 = array(x, 2)

                array(i, 0) = x0
                array(i, 1) = x1
                array(i, 2) = x2

                array(x, 0) = i0
                array(x, 1) = i1
                array(x, 2) = i2
            End If
        Next
    Next
    SortByLen = array
End Function

Verwijderd

Topicstarter
YEA! het is me gelukt, bedankt voor jullie hulp :D
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Function SortByLen(ByVal array)
    Dim i, x
    Dim iLen, xLen
    Dim i0, i1, i2
    Dim x0, x1, x2

    For i = 0 To UBound(array) - 1
        iLen = Len(array(i, 1))

        For x = i + 1 To UBound(array)
            xLen = Len(array(x, 1))

            If xLen > iLen Then
                i0 = array(i, 0)
                i1 = array(i, 1)
                i2 = array(i, 2)

                x0 = array(x, 0)
                x1 = array(x, 1)
                x2 = array(x, 2)

                array(i, 0) = x0
                array(i, 1) = x1
                array(i, 2) = x2

                array(x, 0) = i0
                array(x, 1) = i1
                array(x, 2) = i2

                iLen = Len(x1)
            End If
        Next
    Next
    SortByLen = array
End Function

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 24-05 20:55

Creepy

Tactical Espionage Splatterer

GigaDave56 schreef op 16 juni 2004 @ 09:42:
Typisch geval van Bubble sort... :7
Waarom??

Waarom geen Mergesort? Of een Quicksort? Of geen <willekeurig ander sorteer algoritme>?

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney

Pagina: 1