Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with Custom Sorting Function

Status
Not open for further replies.

fooobee

Technical User
Nov 13, 2003
33
US
I have wrote a function to combine MS Word file from a specified directory into one file. However, it combines the files alphabetically, and this is not what I want. I have custom sorting rules that need to be used. Here they are...

The sort is driven by a string in the file name bounded by the first hyphen from the left, e.g. for the file 06CP02MajDep-ET_pre the sort is driven by the string beginning –ET_

Not every term in the list below will have a file. Some terms may have greater than 1 (and an alpha sort may be used in that case).

-Info_
-TOC_
-AB_
-ES_
-FES
-Intro_
-EtioPath_
-EtioPath-LP
-Epi_
-Epi-LP
-CT_
-CT-LP
-MP_
-MP-LP
-UN_
-UN-LP
-ET_
-ET-LP
-MKT_
-MKT-LP
-TM
-PL_
-Meth_
-BB_
-T0
-T1
-T2
-T3
-T4
-F0
-F1
-F2
All Others

That is the order of the sort. I was wondering if someone can point me in the right direction (as to what class to use) or give me an example of how to start this?

Thanks
 
IComparer

Do a google on how to write your own IComparer class and use that to sort.
 
Here's an example of an IComparer implementation.
Code:
    public class PersonNameComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            // Two nulls are equal
            if (null == x && null == y)
                return 0;

            // Nulls are always less than non-null values
            if (null != x && null == y)
                return 1;
            if (null == x && null != y)
                return -1;
            
            // Convert x & y to their proper type, if possible.
            Person p1 = x as Person;
            Person p2 = y as Person;
            if ((null == p1) || (null == p2))
                throw new ArgumentException("This method only compares objects of type Person");

            string nameFormat = "{0}, {1}";
            string PersonName1 = string.Format(nameFormat, p1.LastName, p1.FirstName);
            string PersonName2 = string.Format(nameFormat, p2.LastName, p2.FirstName);
            return string.Compare(PersonName1, PersonName2, true);
        }
    }

Hope this helps you get started.

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chiph,

How would I begin since I am comparing so many objects? I was thinking of putting the sort sequence into an array and then looping through it to grab the filenames that match that array index and then adding the filenames to another array by that order? I am not sure if this is the best way or not. If not, can you show me a short ex of comparing 3 of my sort sequence (-Info_,-TOC_,-AB_)?
 
You can put them into either an Array or an ArrayList, and then call the Sort() method on it, which takes an IComparer.

I'm not sure which sort algorithm it uses internally, but it's pretty quick in most cases. Just remember that it's going to call your IComparer implementation twice for each compare operation, so make sure your comparer runs fast. :)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top