如何在数字显微镜中对对象列表进行排序

时间:2015-03-16 06:16:49

标签: dm-script

是否可以对ObjectList对象进行排序? 例如:

  class MissPlane: object
{
    number  d_local
    object init(object self, number d)
    {
        d_local=d
        return self
    }
            number getD(object self)
    {
        return d_local
    }
    void print(object self)
    {
        result("d="+d_local+"\n")
    }

}
number h,k,l,hmax
hmax=2
result("start----------------"+datestamp()+"----------------------\n")

Object PlaneList
PlaneList=Alloc(ObjectList)
for(l=-hmax;l<=hmax;l++){
    for(k=-hmax;k<=hmax;k++){
        for(h=-hmax;h<=hmax;h++){
            Object MPObject=Alloc(MissPlane)
            MPObject.init(random())
            PlaneList.AddObjectToList(MPObject)
            MPObject.print()
        }
    }
}

最后我需要按d进行排序。

PS。 DM手册中没有完整记录ObjectList。

2 个答案:

答案 0 :(得分:1)

您可以使用DM命令sort来对 TagLists ObjectList 进行排序。在这两种情况下,您都必须创建一个&#34;排序&#34; class,定义比较两个元素的方法,如果第二个元素大于第一个元素则返回。

以下示例适用于TagLists,对数字tagList进行排序以增加数字。

Class Sorter
{
    // The signature of the following method must not be changed. 
    Number Bigger( Object self, TagGroup TG1, Number index1, TagGroup TG2, Number index2 )
    {
        Number N1, N2
        TG1.TagGroupGetIndexedTagAsNumber( index1, N1 )
        TG2.TagGroupGetIndexedTagAsNumber( index2, N2 )
        return (N1<N2)?1:0
    }
}

TagGroup CreateRandomList( Number n )
{
    TagGroup list = NewTagList()
    For ( Number i = 0 ; i < n ; i++ )
    list.TagGroupInsertTagAsNumber( i, Random() )
    return list
}

void Main()
{
    TagGroup unsorted = CreateRandomList( 10 ) 
    unsorted.TagGroupOpenBrowserWindow( "unsorted", 0 )
    Object sortObject = Alloc( Sorter )
    Sort( 0, unsorted, sortObject, "Bigger" )   // The first parameter specifies ‘stable’ search
    unsorted.TagGroupOpenBrowserWindow( "sorted", 0 )
}
Main()

下一个示例是针对ObejctLists,对objectList进行排序。在这种情况下,列表中的对象只是&#34;包装数字&#34;并且再次获得的数量越来越多。

class MyObject
{
    Number value
    Object Init( Object self, Number in ) { value = in; return self; }
    Number Get( Object self ) { return value; }
}

Class Sorter
{
    // The signature of the following method must not be changed.
    Number HigherValue( Object self, Object obj1, Object obj2 ) 
    {
        return ( obj1.Get() < obj2.Get() ) ? 1 : 0
    }
}

Object CreateRandomList( Number n )
{
    Object obList = Alloc(ObjectList)
    For ( Number i = 0 ; i < n ; i ++ )
    {
        Object newOBJ = Alloc(MyObject).Init( Random() )
        obList.AddObjectToList( newOBJ )
    }
    return obList
}

void PrintObjects( Object obList )
{
    ForEach( Object ob; obList )
        Result( "value:" + ob.Get() + "\n" )
}

void Main()
{
    Object unsorted = CreateRandomList( 7 ) 
    Result( "\nBefore sort:\n" )
    unsorted.PrintObjects()
    Result( "\nAfter sort:\n" )
    Sort( 0, unsorted, Alloc(Sorter), "HigherValue" )
    unsorted.PrintObjects()
}
Main()

这两个代码示例均来自this website

答案 1 :(得分:0)

以下示例创建一个简单的“bubble-sort”方法来对对象列表进行排序:

 class MissPlane: object
{
    number  d_local
    object init(object self, number d)
    {
        d_local=d
        return self
    }
            number getD(object self)
    {
        return d_local
    }
    void print(object self)
    {
        result("d="+d_local+"\n")
    }

}

object sortList( object in )
{
    object copy = in.ScriptObjectClone()    // Making a copy avoids messing with your original list...
    object sorted=Alloc(objectlist)

    while( copy.SizeOfList() )
    {
        object t = copy.ObjectAt(0)
        for (number c=1; c<copy.SizeOfList(); c++ )
        {
            if ( t.GetD() > copy.ObjectAt(c).GetD() ) 
                t = copy.ObjectAt(c);   // found a "smaller" object
        }
        sorted.AddObjectToList(t)   // add the smallest item found
        copy.RemoveObjectFromList(t)
    }

    return sorted
}

number h,hmax
hmax=20
result("start----------------"+datestamp()+"----------------------\n")

Object PlaneList
PlaneList=Alloc(ObjectList)
for(h=-0;h<hmax;h++){
    Object MPObject=Alloc(MissPlane)
    MPObject.init(random())
    PlaneList.AddObjectToList(MPObject)
}

result("start---------------- ORIGINAL ----------------------\n")
ForEach( object o; PlaneList )
    o.print()
Object sorted = sortList(PlaneList)
result("start---------------- SORTED ----------------------\n")
ForEach( object o; sorted)
    o.print()