美文网首页
ArrayList排序问题

ArrayList排序问题

作者: 蓝色Hippie | 来源:发表于2018-01-20 16:51 被阅读9次

ArrayList集合,包含默认的方法Sort(),这个方法使用时可以不带参数,此时使用默认的比较方式,也可以给它传送IComparer接口。

给ArrayList填充了简单类型时,例如整数或者字符串,就会进行默认的比较。对于自己的类,必须在类定义中实现IComparable,或创建一个支持IComparer的类,来进行比较。

1.Person.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication2

{

public class Person:IComparable

{

    public string Name;

    public int Age;

    public Person(string name, int age)

    {

        Name = name;

        Age = age;

    }

    public int CompareTo(object obj)

    {

        if (obj is Person)

        {

            Person otherPerson = obj as Person;

            return this.Age - otherPerson.Age;

        }

        else {

            throw new ArgumentException("Object to compare to  is not a Person object ");

        }

    }

}

}

2.PsersonCompareName.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

namespace ConsoleApplication2

{

public class PsersonCompareName:IComparer

{

    public static IComparer Default = new PsersonCompareName();

    public int Compare(object x, object y)

    {

        if (x is Person && y is Person)

        {

            return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);

        }

        else {

            throw new ArgumentException("error");

        }

    }

}

}

3.调用

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

namespace ConsoleApplication2

{

class Program

{

    static void Main(string[] args)

    {

        ArrayList list = new ArrayList();

        list.Add(new Person("R",30));

        list.Add(new Person("D", 25));

        list.Add(new Person("M", 27));

        list.Add(new Person("B", 44));

        Console.WriteLine("Unsorted people:");

        for (int i = 0; i < list.Count; i++)

        {

            Console.WriteLine((list[i] as Person).Name + "  " + (list[i] as Person).Age);

        }

        Console.WriteLine("default sort");

        list.Sort();

        for (int i = 0; i < list.Count; i++)

        {

            Console.WriteLine((list[i] as Person).Name + "  " + (list[i] as Person).Age);

        }

        Console.WriteLine("not default sort");

        list.Sort(PsersonCompareName.Default);

        for (int i = 0; i < list.Count; i++)

        {

            Console.WriteLine((list[i] as Person).Name + "  " + (list[i] as Person).Age);

        }

    }

}

}


2.png

相关文章

  • ArrayList排序问题

    ArrayList集合,包含默认的方法Sort(),这个方法使用时可以不带参数,此时使用默认的比较方式,也可以给它...

  • Head First Java 16 数据结构

    数据结构 排序 ArrayList没有sort()方法,ArrayList有add(index, element)...

  • ArrayList排序

    ArrayList排序:Comparator 效果图:

  • ArrayList排序

    Collections.sort(gradeclassnameList, newSortByAge()); for...

  • ArrayList说我也想要排序(一)

    一次面试被问到ArrayList如果要进行排序,该怎么办?当时我就被问懵了,ArrayList不是没有排序功能的吗...

  • 2018-04-26

    Java学习随笔8 List排序 这里的list默认是ArrayLIst 首先ArrayList有两种sort方法...

  • ArrayList对象排序

    前言:最近对收音机的开发,遇到一个需求,将收藏列表显示在前,电台列表显示在后,所以需要对列表进行对象排序,在此做一...

  • ArrayList等常见集合的排序问题

    对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体...

  • 集合排序

    ArrayList存放学生对象及排序 【问题描述】 编写一个学生类,包含姓名、学号、年龄、专业四个属性,并完善ge...

  • 【Java】对象排序问题

    应用场景:ArrayList里存储一坨对象,按照其中的某个属性,对整个ArrayList进行排序 对包含一列som...

网友评论

      本文标题:ArrayList排序问题

      本文链接:https://www.haomeiwen.com/subject/miptaxtx.html