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

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

博客园 - 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---
        }

    }

}