08. oktober 2003 - 17:09
#7
okay, problemet er:
jeg har en combobox hvor datakommer fra et array...
det array er sorteret forkert... Hvordan sortere jeg i det array, så jeg får det højeste tal først og det højeste tal er valgt?
10. oktober 2003 - 12:17
#9
Ville det ikke være nemmerer at bruge VB.net egen "Sort" ?
Eks. :
Dim a As Integer() // array af integer
Array.Sort(a) // sorterer
Nb. du kan soreterer på forskellige måder - se doc. for hjælp
Imports System
Imports Microsoft.VisualBasic
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array.
Dim myArray As Array = Array.CreateInstance(GetType(String), 9)
myArray.SetValue("The", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumped", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
' Displays the values of the Array.
Console.WriteLine("The Array initially contains the " _
+ "following values:")
PrintIndexAndValues(myArray)
' Sorts the values of the Array.
Array.Sort(myArray)
' Displays the values of the Array.
Console.WriteLine("After sorting:")
PrintIndexAndValues(myArray)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces the following output.
'
' The Array initially contains the following values:
' [0]: The
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumped
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' After sorting:
' [0]: brown
' [1]: dog
' [2]: fox
' [3]: jumped
' [4]: lazy
' [5]: over
' [6]: quick
' [7]: the
' [8]: The
[C#]
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a new Array.
Array myArray=Array.CreateInstance( typeof(String), 9 );
myArray.SetValue( "The", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumped", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
// Displays the values of the Array.
Console.WriteLine( "The Array initially contains the following values:" );
PrintIndexAndValues( myArray );
// Sorts the values of the Array.
Array.Sort( myArray );
// Displays the values of the Array.
Console.WriteLine( "After sorting:" );
PrintIndexAndValues( myArray );
}
public static void PrintIndexAndValues( Array myArray ) {
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
}
/*
This code produces the following output.
The Array initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
After sorting:
[0]: brown
[1]: dog
[2]: fox
[3]: jumped
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The
*/
[JScript]
import System;
// Creates and initializes a new Array.
var myArray : System.Array= System.Array.CreateInstance( System.String, 9 );
myArray.SetValue( "The", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumped", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
// Displays the values of the Array.
Console.WriteLine( "The Array initially contains the following values:" );
PrintIndexAndValues( myArray );
// Sorts the values of the Array.
System.Array.Sort( myArray );
// Displays the values of the Array.
Console.WriteLine( "After sorting:" );
PrintIndexAndValues( myArray );
function PrintIndexAndValues( myArray : System.Array ) {
for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
/*
This code produces the following output.
The Array initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
After sorting:
[0]: brown
[1]: dog
[2]: fox
[3]: jumped
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The
*/
[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button in the upper-left corner of the page.
Hilsen Grasp