how can I order the elements of a listview without the sort function (visual basic 2008 & compact net framework 2.0)

205 views Asked by At

I'm working on a Windows CE form with inside a listview this listview is composed of two text columns which represent, respectively, a numerical code and the corresponding description, for example:

Code Code Description

01 | Red lamp off

89 | stackoverflow is a cool community

03 | pump 01 on

00 | Red lamp flashing

and so on

I inserted within the form a combobox populated with numeric values in text format from "00" to "99" and a Textbox where you can write freely.

by means of two command buttons I can insert inside the listview a new combination of code/description element, modify the text of a code already inserted, or delete the entire line composed by the code and the text associated to it. listview.

'--- Functions Used by me ---

'creating a new element inside the listview

Dim ListCode As New ListViewItem("00")

ListCode.SubItems.Add("Red lamp flashing")
myListView.Items.Add(ListCode)

'deleting an element inside the listview

myListView.Items(lvPosition).Remove()

'--- 000 ---

So far so good (Bryan Adams)

The problems start now Not working on a windows form but with a Windows CE form (.net compact framework), I do not have the selectable sort property available in the visual studio properties window I need to apply a selection sorting algorithm so as to obtain the elements in ascending order:

00 | Red lamp flashing

01 | Red lamp off

03 | pump 01 on

89 | stackoverflow is a cool community

programming in visual basic .NET and not in old VB "I think" to be handling the arrays of objects and not simple strings, am I wrong?

2

There are 2 answers

0
Phantom Lord On

this is the simplest algorithm for sorting elements in a Long Array[0..n]:

for i = 0 to n-1
  for j = i + 1 to n
    if myArray[i] > myArray[j] then 'swap items
      k = myArray[j];
      myArray[j] = myArray[i];
      myArray[i] = k;
    next j
next i

adapt

0
rhizopode On
Private Sub SortListview() 'sorting elements inside the listview
    Dim i, j
    Dim ArrayTemp() As String = {"", ""} 'support array


    For i = 0 To myListview.Items.Count - 2
        For j = i + 1 To myListview.Items.Count - 1
            If Val(myListview.Items.Count(i).Text) > Val(myListview.Items.Count(j).Text) Then
                ArrayTemp(0) = myListview.Items.Count(i).Text
                ArrayTemp(1) = myListview.Items.Count(j).SubItems(1).Text

                myListview.Items.Count(j).Text = myListview.Items.Count(i).Text
                myListview.Items.Count(j).SubItems(1).Text = myListview.Items.Count(i).SubItems(1).Text

                myListview.Items.Count(i).Text = ArrayTemp(0)
                myListview.Items.Count(i).SubItems(1).Text = ArrayTemp(1)

            End If

        Next
    Next
End Sub

Algorithm applied to the listview and working correctly

Is there a more elegant and compact way to write this code?

Thanks for your help guys!