Phn lo?i m?t t? i?n k?ch b?n dn c v?i cc d? li?u chu?i

D?ch tiu ? D?ch tiu ?
ID c?a bi: 246067
Bung t?t c? | Thu g?n t?t c?

TM T?T

M?t k?ch b?n t? i?n l tng ng v?i m?t m?ng k?t h?p PERL, ni m thng tin ?c lu tr? trong m?t m?ng ch?a duy nh?t phm v m?c d? li?u. B?i v? thng tin ny ?c lu tr? unsorted, li?t k thng qua m?ng tr? v? thng tin theo th? t? n ?c lu tr?. M?c ch c?a bi vi?t ny l ? xc ?nh cc SortDictionary() ch?c nng s?p x?p cc n?i dung c?a m?t ?i t?ng t? i?n c dn c v?i cc d? li?u chu?i phm ho?c m?c.

THNG TIN THM

V d? m? sau ?nh ngh?a hai h?ng, dictKey v dictItem, ?c s? d?ng b?i cc SortDictionary() ch?c nng. Cc SortDictionary() ch?c nng ph?i m?t hai tham s?, m?t v?t th? tr?c y t?o ra nh l m?t t? i?n k?ch b?n v l m?t trong cc h?ng s? ? xc ?nh li?u ? s?p x?p theo cc t? i?n phm ho?c m?c.

Const dictKey  = 1
Const dictItem = 2

Function SortDictionary(objDict,intSort)
  ' declare our variables
  Dim strDict()
  Dim objKey
  Dim strKey,strItem
  Dim X,Y,Z

  ' get the dictionary count
  Z = objDict.Count

  ' we need more than one item to warrant sorting
  If Z > 1 Then
    ' create an array to store dictionary information
    ReDim strDict(Z,2)
    X = 0
    ' populate the string array
    For Each objKey In objDict
        strDict(X,dictKey)  = CStr(objKey)
        strDict(X,dictItem) = CStr(objDict(objKey))
        X = X + 1
    Next

    ' perform a a shell sort of the string array
    For X = 0 to (Z - 2)
      For Y = X to (Z - 1)
        If StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0 Then
            strKey  = strDict(X,dictKey)
            strItem = strDict(X,dictItem)
            strDict(X,dictKey)  = strDict(Y,dictKey)
            strDict(X,dictItem) = strDict(Y,dictItem)
            strDict(Y,dictKey)  = strKey
            strDict(Y,dictItem) = strItem
        End If
      Next
    Next

    ' erase the contents of the dictionary object
    objDict.RemoveAll

    ' repopulate the dictionary with the sorted information
    For X = 0 to (Z - 1)
      objDict.Add strDict(X,dictKey), strDict(X,dictItem)
    Next

  End If

End Function
				

A lm vi?c v d?

V d? sau s? d?ng c?a cc ch?c nng ? trn.

Lu y: T?t c? cc ? ki?n ? ?c g? b? t? ch?c nng phn lo?i th?c t? trong v d? ny.

Copy o?n m? sau ASP v lu n vo m?t th m?c nh Sortdemo.asp v?i t script truy nh?p:
<%@LANGUAGE="VBSCRIPT"%>
<% Option Explicit %>
<html>
<head><title>Dictionary Sorting</title></head>
<body>
<%

  Dim d, i

  Const dictKey  = 1
  Const dictItem = 2

  Set d = Server.CreateObject("Scripting.Dictionary")

  d.Add "3", "Delta"
  d.Add "1", "Foxtrot"
  d.Add "4", "Bravo"
  d.Add "2", "Echo"
  d.Add "6", "Alpha"
  d.Add "5", "Charlie"

  Response.Write "<p>Before Sorting:<br>"
  For Each i In d
    Response.Write i & "=" & d(i) & "<br>"
  Next

  Response.Write "<p>By Key:<br>"
  SortDictionary d,dictKey
  For Each i In d
    Response.Write i & "=" & d(i) & "<br>"
  Next

  Response.Write "<p>By Item:<br>"
  SortDictionary d,dictItem
  For Each i In d
    Response.Write d(i) & "=" & i & "<br>"
  Next
%>
</body>
</html>
<%
  Function SortDictionary(objDict,intSort)
    Dim strDict()
    Dim objKey
    Dim strKey,strItem
    Dim X,Y,Z
    Z = objDict.Count
    If Z > 1 Then
      ReDim strDict(Z,2)
      X = 0
      For Each objKey In objDict
          strDict(X,dictKey)  = CStr(objKey)
          strDict(X,dictItem) = CStr(objDict(objKey))
          X = X + 1
      Next
      For X = 0 to (Z - 2)
        For Y = X to (Z - 1)
          If StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0 Then
              strKey  = strDict(X,dictKey)
              strItem = strDict(X,dictItem)
              strDict(X,dictKey)  = strDict(Y,dictKey)
              strDict(X,dictItem) = strDict(Y,dictItem)
              strDict(Y,dictKey)  = strKey
              strDict(Y,dictItem) = strItem
          End If
        Next
      Next
      objDict.RemoveAll
      For X = 0 to (Z - 1)
        objDict.Add strDict(X,dictKey), strDict(X,dictItem)
      Next
    End If
  End Function
%>
				
Khi duy?t, ?u ra sau y s? ?c hi?n th?:
Before Sorting:
3=Delta
1=Foxtrot
4=Bravo
2=Echo
6=Alpha
5=Charlie

By Key:
1=Foxtrot
2=Echo
3=Delta
4=Bravo
5=Charlie
6=Alpha

By Item:
Alpha=6
Bravo=4
Charlie=5
Delta=3
Echo=2
Foxtrot=1
				

Microsoft cung c?p l?p tr?nh v d? ? minh ho? ch?, khng c b?o hnh ho?c th? hi?n hay ng? ?. i?u ny bao g?m, nhng khng gi?i h?n, b?o ?m ng? ? kh? nng bn hng ho?c cho m?t m?c ch c? th?. Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i ngn ng? l?p tr?nh m ang ?c ch?ng minh v v?i nh?ng cng c? ?c s? d?ng ? t?o ra v g? l?i th? t?c. Microsoft h? tr? cc k? s c th? gip gi?i thch cc ch?c nng c?a m?t th? t?c c? th?, nhng h? s? khng s?a ?i cc v d? ? cung c?p thm ch?c nng ho?c xy d?ng quy tr?nh ? p ?ng cc yu c?u c? th? c?a b?n.
https://Partner.Microsoft.com/Global/30000104
? bi?t thm v? cc ty ch?n h? tr? c s?n v v? lm th? no ? lin h? v?i Microsoft, gh thm Web site sau c?a Microsoft:
http://support.Microsoft.com/Default.aspx?SCID=FH;EN-US;CNTACTMS

THAM KH?O

? bi?t thm chi ti?t v? cng ngh? k?ch b?n ny c?a Microsoft, xem trang web Microsoft Developer Network Web t?i URL sau:
http://msdn2.Microsoft.com/en-US/Library/ms950396.aspx

Thu?c tnh

ID c?a bi: 246067 - L?n xem xt sau cng: 21 Thang Tam 2011 - Xem xt l?i: 2.0
T? kha:
kbinfo kbmt KB246067 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:246067
Khc t Ni dung trong C s Kin thc Khng con c h tr
Bi vi?t ny ni v? cc s?n ph?m m Microsoft khng c?n h? tr? n?a. Do , bi vi?t ny ?c cung c?p "nguyn b?n" v s? khng ?c c?p nh?t.

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com