惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
GbyAI
GbyAI
爱范儿
爱范儿
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
罗磊的独立博客
Recent Announcements
Recent Announcements
博客园 - 司徒正美
M
MIT News - Artificial intelligence
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog RSS Feed
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网

博客园 - Caviare

前台优化法则(YSlow) ASP.NET MVC源代码 启用IIS的Gzip压缩功能 web.sitemap Dynamic Generation SQL字符正则匹配 委托应用 WorkBook操作实例 判断大陆IPAddress Bertrand Meyer提出的设计模式的原则 - Caviare - 博客园 Web.cofig详解[转] UpdatePanel Control [转老赵博客] .Net环境下的缓存技术介绍 (转) [转]webservice和remoting在分布式程序中的应用 sql2005备份在sql2000中恢复 Excel WorkBook操作例 温故知新-Excel操作类 SQL collation设置 一张类的访问权限图,帮您加深概念 [转]您可能不知道的Asp.Net2.0技巧
迭代器和排序基本使用
Caviare · 2007-11-02 · via 博客园 - Caviare

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---
        }

    }

}