0

I have implemented my own selection sort method that seems to be doing it's job for the most part; However, when I am printing files to an excel sheet the printer does not print the first item. I am unsure whether or not the sort method is the source of the problem. My test method for my sort method passes, which is why I am doubting that that is the source. My sort method is shown below. Does it have an error in the scope or order or operations? When I manually move through it on paper everything sorts properly.

    public bool sortMaterial() 
    {
        for (int i = 0; i < salesList.Count - 2; i++)
        {
            Sales curr = salesList[i];
            Sales temp;
            Sales min = curr;
            int swap = 0;

            for (int j = i + 1; j < salesList.Count; j++ )
            {
                temp = salesList[j];

                if (String.Compare(temp.material, min.material) == -1)
                {
                    min = temp;
                    swap = j;
                }
            }

            salesList[i] = min;
            salesList[swap] = curr;
        }

        return true;
    }
Alex Bochel
  • 60
  • 1
  • 11
  • 1
    What has your debugging shown? – rory.ap Jun 12 '17 at 14:07
  • for (int i = 0; i < salesList.Count - 1; i++) : Seems better... but the overall approach is not right... Bubble sort is shown here : https://stackoverflow.com/questions/14768010/simple-bubble-sort-c-sharp for example. The example of Nikoo seems correct... skip other s{###[{# code :-) – Laurent Lequenne Jun 12 '17 at 14:08
  • Why does this method return a boolean that's always true? Seems like you want it to return a void instead. – Toolmaker Jun 12 '17 at 14:25
  • @LaurentLequenne I do not wish to do a bubble sort as it is a very inefficient method of sorting. When I use -1 instead of -2 my method stops working in my tests. I also had thought -1 was necessary and was surprised when it did not work – Alex Bochel Jun 12 '17 at 14:28
  • I don't know what kind of sort you are implementing... but it just looks like a wrong implementation of the bubble sort doing even less then it has to :-) – Laurent Lequenne Jun 12 '17 at 14:53
  • @LaurentLequenne I am implementing a selection sort which doesn't look as bad as the bubble sort you linked to still has a better efficiency; However, I may just try it for the heck of it since something is not working for me. – Alex Bochel Jun 12 '17 at 15:01
  • Ok Alex... but selection or bubble sort... your implementation is not correct... Anyway Linq or .NET sort do it for you :-) – Laurent Lequenne Jun 13 '17 at 11:31

1 Answers1

1

A neat way to do custom sorting is by implementing the IComparer<T> interface:

public class SalesMaterialComparer : IComparer<Sales>  {
    public int Compare(Sales x, Sales y) {
        return String.Compare(x.material, y.material);
    }
}

You can pass your custom comparer to the LINQ OrderBy() method.

IEnumerable<Sales> salesList;
var myComparer = new SalesMaterialComparer();

var sorted = salesList.OrderBy(s => s, myComparer);
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36