using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testApp
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
            

        }

        
private void button1_Click(object sender, EventArgs e)
        
{
            usingIteration();
            usingComparer();
            
        }


        
/// <summary>
        
/// 迭代器使用Demo
        
/// </summary>

        void usingIteration()
        
{
            System.Text.StringBuilder sbShow 
= new StringBuilder();
            System.Collections.IEnumerator ie 
= this.comboBox1.Items.GetEnumerator();

            
while (ie.MoveNext())
            
{
                sbShow.AppendLine(ie.Current.ToString());
            }

            MessageBox.Show(sbShow.ToString());
            
/*
            ---line1---
            ---line5---
            ---line3---
            ---line2---
            ---line4---
            ---line7---
            ---line8---
            ---line6---
             
*/

        }


        
/// <summary>
        
/// LIST排序Demo
        
/// </summary>

        void usingComparer()
        
{
            StringSort sort 
= new StringSort();
            System.Collections.ArrayList arrList 
= new System.Collections.ArrayList(comboBox1.Items);
            arrList.Sort(sort);
            System.Text.StringBuilder sbShow 
= new StringBuilder();
            
for (int i = 0; i < arrList.Count; i++)
            
{
                sbShow.AppendLine(arrList[i].ToString());
            }

            MessageBox.Show(sbShow.ToString());
            
/*
            ---line1---
            ---line2---
            ---line3---
            ---line4---
            ---line5---
            ---line6---
            ---line7---
            ---line8---
             
*/

        }

    }


    
public class StringSort : System.Collections.IComparer
    
{
        
public int Compare(object x, object y)
        
{
            
string str1 = x as string;
            
string str2 = y as string;
            
if (str1 == null || str2 == null)
            
{
                
throw new Exception("Item Error!");
            }

            
else
            
{
                
if (intGetIndex(str1) < intGetIndex(str2))
                
{
                    
return -1;
                }

                
else
                
{
                    
return 0;
                }

            }

        }

        
int intGetIndex(string str)
        
{
            
return Convert.ToInt32(str.Substring(71));//---line1---
        }

    }

}
posted on 2007-11-02 17:31  Caviare  阅读(895)  评论(0编辑  收藏  举报