1

I am having problem with List Object values. Object is an object of class which holding two integer number.

like

List<Object> container;
container.Add(new Coordinates(x_axis, y_axis));  // x_axis=200, y_axis=300

I have to add unique coordinates each time, means next time x_axis, y_axis can never be added to list if it is 200, 300 respectively.

How can I check the already exist item in list objects?

Thanks


HTML simple list sorting

is there is simple way to sort only the outer level of multilist level in HTML? (it's not important to sort the inner level of the lists) If possible just javascript code, no jQuery since i never used it.

Example: Before sorting

  1. List Z

    • Katty
    • Della
  2. List G

    • Test
    • fox
  3. List R

    • ABC
    • AAC

After sorting:

  1. List G

    • Test
    • fox
  2. List R

    • ABC
    • AAC
  3. List Z

    • Katty
    • Della

Thank you in advance Mike

After using the Jquery method answered below, i got error "Object does not support this property or method??

Update: here is acode sample:

<head>

<script LANGUAGE="JavaScript" SRC="./js/jquery-1.7.2.js"></SCRIPT>
<script type="text/javascript">



    function sortDesc()
    {
        $('ul.list > li').sortElements(function(a, b){
        return $(a).text() > $(b).text() ? 1 : -1;});
    }

</script>
</head>

<body>
    <div id="container">
        <div id="main">
            <div class="c1">
                <div id="example-list">
                    <table cellspacing="0" cellpadding="0" border="0" width="100%">
                        <tr>
                            <td align="middle"     align="center" title='sort' style="cursor:pointer" valign="middle" align="center"  style="width:20px; height:20px;">
                                <img border="0" src="./js/r_oinfo.gif" width="20" height="20"  onclick="sortDesc()">
                        </td>
                    </table>
                    <ul class="list">
                        <li>
                            Search

                        </li>
                        <ul>
                            <li>
                                se 1
                            </li>
                            <li>
                                se 2
                            </li>
                            <li>
                                se 0
                            </li>
                        </ul>
                        <li>
                            Sort
                        </li>
                        <ul>
                            <li>
                                so 1
                            </li>
                            <li>
                                so 0
                            </li>
                        </ul>
                        <li>
                            Filter

                        </li>
                    </ul>
                 </div>
            </div>

        </div>
    </div>
</body>
</html>
4

5 回答 5

3

改用 a List<Coordinates>

要检查列表中是否存在一组坐标,您必须遍历列表并比较属性值:

Coordinates point = new Coordinates(200, 300);

if (!container.Any(c => c.x_axis == point.x_axis && c.y_axis = point.y_axis)) {
  container.Add(point);
}
于 2012-04-16T08:16:58.740 回答
3

覆盖 Coordinates 类的 Equals 和 GetHashCode:

public class Coordinates
{
    public Coordinates(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int X { get; private set; }
    public int Y { get; private set; }

    public override bool Equals(object obj)
    {
        if (!(obj is Coordinates))
        {
            return false;
        }
        Coordinates coordinates = (Coordinates)obj;
        return ((coordinates.X == this.X) && (coordinates.Y == this.Y));
    }

    public override int GetHashCode()
    {
        return (X ^ Y);
    }
}

并使用您的 Coordinates 对象的通用 List 或 HashSet :

List<Coordinates> container = new List<Coordinates>();
Coordinates coordinates = new Coordinates(x_axis, y_axis);

if (!container.Contains(coordinates)
    container.Add(coordinates);

并使用 HashSet:

HashSet<Coordinates> container = new HashSet<Coordinates>();
container.Add(new Coordinates(x_axis, y_axis));
于 2012-04-16T08:29:40.190 回答
0

您可以将您从容器中拉出的项目投射回坐标,然后检查它们的 x 和 y 值。或者,如果它继承自 ICombparable,则只需使用 = 运算符。如果它不是 IComparable,您可以创建自己的类,并继承 IComparable 接口。

    var myCoord = (Coordinates) container(0);

这一切都假设你真的不想让你的容器直接持有坐标。

编辑:修正了一堆错别字

于 2012-04-16T08:13:10.620 回答
0

您可以像这样使用 LINQ:

if (!container.Any(c => (Coordinates)c.x_axis == x_axis &&
                        (Coordinates)c.y_axis == y_axis))
    container.Add(new Coordinates(x_axis, y_axis));
于 2012-04-16T08:15:23.840 回答
0

您最好使用HashSetDictionary(如果您需要将它们按特定顺序保存)以确保它们确实是唯一的。

然后你应该重写GetHashCodeEquals来检查相等性。

GetHashCode 的最佳实践:覆盖 System.Object.GetHashCode 的最佳算法是什么?

执行

public class Coordinate
{
    public Int32 X { get; set; }
    public Int32 Y { get; set; }

    public override int GetHashCode()
    {
        Int32 hash = 17;
        hash = hash * 23 + X;
        hash = hash * 23 + Y;
        return hash;
    }
    public override Boolean Equals(object obj)
    {
        Coordinate other = obj as Coordinate;
        if (other != null)
        {
            return (other.X == X) && (other.Y == Y);
        }
        return false;
    }
}

字典

Int32 count = 0;
Dictionary<Coordinate, Int32> cords = new Dictionary<Coordinate, Int32>();
// TODO : You need to check if the coordinate exists before adding it
cords.Add(new Coordinate() { X = 10, Y = 20 }, count++);
// To get the coordinates in the right order
var sortedOutput = cords.OrderBy(p => p.Value).Select(p => p.Key);

哈希集

HashSet<Coordinate> cords = new HashSet<Coordinate>();
cords.Add(new Coordinate() { X = 10, Y = 20 });
于 2012-04-16T08:29:22.047 回答