سؤال

In Metro Style apps sometimes we use Platform::Collections::Vector to hold elements used in a ListView.

How to sort a Platform::Collections::Vector?

I'm aware there are plenty of structures in std that can be sorted but I was wondering if there was some method for Platform::Collections::Vector other than writing your own sort function.

هل كانت مفيدة؟

المحلول

Actually something like the below should also work:

auto vec = ref new Platform::Collections::Vector<T^>();
std::sort(begin(vec), end(vec));

نصائح أخرى

I didn't find any suitable answer so I used this workaround.

It's a simple quicksort over a Platform::Collections::Vector

void swap (Platform::Collections::Vector<T^>^ vec, int pos1, int pos2)
{
   T^ tmp = vec->GetAt(pos1);
   vec->SetAt(pos1, vec->GetAt(pos2));
   vec->SetAt(pos2,tmp);
}

int compare (T^ c1, T^ c2)
{
   int c = wcscmp (c1->Title->Data(),c2->Title->Data());
   return -c;
}

int PartitionVec (int left, int right, 
                            Platform::Collections::Vector<T^>^ vec)
{
   int i,j;
   i = left;
   for (int j = left + 1; j <= right; ++j)
   {
       if (compare (vec->GetAt(j),vec->GetAt(left)) > 0)
       {
               ++i;
           swap (vec,i,j);
       }
    }
    swap (vec,left,i);
    return i;
}

void QuickSortVec (Platform::Collections::Vector<T^>^ vec,
                                           int start, int end)
{
   if (end > start)
   {
       int pivot_point;
       pivot_point = PartitionVec (start, end, vec);
       QuickSortVec (vec,start,pivot_point - 1);
       QuickSortVec (vec, pivot_point + 1, end);
    } 
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top