Bubble sort object array : Sort « Data Structure « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Data Structure » Sort 
11.47.1.Bubble sort object array
using System;

delegate bool CompareOp(object lhs, object rhs);

class MainEntryPoint {
    static void Main() {
        Employee[] employees = {
              new Employee("B"20000),
              new Employee("E"10000),
              new Employee("D"25000),
              new Employee("W"(decimal)1000000.38),
              new Employee("F"23000),
              new Employee("R'"50000)};
        CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
        BubbleSorter.Sort(employees, employeeCompareOp);

        for (int i = 0; i < employees.Length; i++)
            Console.WriteLine(employees[i].ToString());
        Console.ReadLine();
    }
}

class Employee{
    private string name;
    private decimal salary;

    public Employee(string name, decimal salary) {
        this.name = name;
        this.salary = salary;
    }

    public override string ToString() {
        return string.Format(name + ", {0:C}", salary);
    }

    public static bool RhsIsGreater(object lhs, object rhs) {
        Employee empLhs = (Employee)lhs;
        Employee empRhs = (Employee)rhs;
        return (empRhs.salary > empLhs.salarytrue false;
    }
}

class BubbleSorter {
    static public void Sort(object[] sortArray, CompareOp gtMethod) {
        for (int i = 0; i < sortArray.Length; i++) {
            for (int j = i + 1; j < sortArray.Length; j++) {
                if (gtMethod(sortArray[j], sortArray[i])) {
                    object temp = sortArray[i];
                    sortArray[i= sortArray[j];
                    sortArray[j= temp;
                }
            }
        }
    }
}
11.47.Sort
11.47.1.Bubble sort object array
11.47.2.Your own quick sort
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.