Algorithm for ordering a list of Objects

∥☆過路亽.° 提交于 2019-12-10 14:17:51

问题


Say you have a List of objects. The User uses mostly all objects when he is working. How can you order the list of objects, so that the list adapts to the order, the users uses mostly? What algorithm can you use for that?

EDIT: Many answers suggested counting the number of times an object was used. This does not work, because all objects are used the same amount, just in different orders.


回答1:


Inside your object, keep a usedCount. Whenever the object is used, increase this count. Then you can simply do this:

objects.OrderByDescending(o => o.UsedCount);



回答2:


I would keep a running count of how many times the object was used, and in what order it was used.

So if object X was used 3rd, average it with the running count and use the result as it's position in the list.

For example:

Item       Uses     Order of Use
---------------------------------------
Object X   10       1,2,3,1,2,1,3,1,2,2 (18)
Object Y   10       3,1,2,3,3,3,1,3,3,1 (23)
Object Z   10       2,3,1,2,1,2,2,2,2,3 (20)

Uses would be how many times the user used the object, the order of use would be a list (or sum) of where the item is used in the order.

Using a list of the each order individually could have some performance issues, so you may just want to keep a sum of the positions. If you keep a sum, just add the order to that sum every time the object is used.

To calculate the position, you would then just use the sum of the positions, divided by the number of uses and you'd have your average. All you would have to do at that point is order the list by the average.

In the example above, you'd get the following averages (and order):

Object X   1.8
Object Z   2.0
Object Y   2.3



回答3:


Add a list of datetimes of when a user accesses an object. Each time a user uses an object, add a datetime.

Now just count the number of datetime entries in your list that are w (now - x days) and sort by that. You can delete the datetimes that are > (now - x days).

It's possible that a user uses different items in a month, this will reflect those changes.




回答4:


You can add a number_of_views field to your object class, ++ it every time the object's used and sort list by that field. And you should make this field=0 to all objects when number_of_views at all objects is the same but isn't 0.




回答5:


I would also use a counter for each object to monitor its use, but instead of reordering the whole list after each use, I would recommend to just sort the list "locally". Like in a bubble sort, I would just compare the object whose counter was just increased with the upper object, and swap them if needed. If swapped, I would then compare the object and its new upper object and so on.

However, it is not very different from the previous methods if the sort is properly implemented.




回答6:


If your User class looks like so:

class User  
{  
    Collection<Algo> algosUsed = new List<Algo>();     //Won't compile, used for explanation
    ...
}  

And your Algo class looks like so:

class Algo  
{  
    int usedCount;  
...  
}  

You should be able to bind specific instances of the Algo object to the User object that allow for the recording of how often it is used. At the most basic level you would serialize the information to a file or a stream. Most likely you want a database to keep track of what is being used. Then when you grab your User and invoke a sort function you order the algos param of User by the usedCount param of Algo




回答7:


Sounds like you want a cache. I spose you could look at the algorithms a cache uses and then take out the whole business about context switching...there is an algorithm called "clock sweep"... but meh that might all be too complex for what you are looking for. To go the lazy way I'd say just make a hash of "used thing":num_of_uses or, in your class, have a var you ++ each time the object is used.

Every once and a while sort the hash by num_of_uses or the objects by the value of their ++'d variable.




回答8:


From https://stackoverflow.com/a/2619065/1429439 :

maybe use OrderedMultiDictionary with the usedCount as the keys and the object as the value.




回答9:


EDIT: ADDED A Order Preferrence!!! look in CODE

I dont like the Last used method as Carra said because it inflict many sort changes which is confusing.

the count_accessed field is much better, though i think it should be levelled to
how many times the user accessed this item in the last XX minutes/hours/days Etc...

the best Datastructure for that is surely

    static TimeSpan TIME_TO_LIVE;
    static int userOrderFactor = 0;

    LinkedList<KeyValuePair<DateTime, int>> myAccessList = new     LinkedList<KeyValuePair<DateTime, int>>();

    private void Access_Detected()
    {
        userOrderFactor++;
        myAccessList.AddLast(new KeyValuePair<DateTime, int>(DateTime.Now, userOrderFactor));
        myPriority += userOrderFactor; // take total count differential, so we dont waste time summing the list
    }



    private int myPriority = 0;
    public int MyPriority
    {
        get
        {
            DateTime expiry = DateTime.Now.Subtract(TIME_TO_LIVE);
            while (myAccessList.First.Value.Key < expiry)
            {
                myPriority += myAccessList.First.Value.Value; // take care of the Total Count 
                myAccessList.RemoveFirst();
            }
            return myPriority;
        }
    }

Hope this helps... it is almost always O(1) BTW...
reminds me somewhat of the Sleep mechanism of Operating Systems




回答10:


When a user interacts with an object, save the ID of the previous object acted upon on that second object so that you always have a pointer to the object used before any given object.

Additionally, store the ID of the most frequently first used object so you know where to start.

When you are building your list of objects to display, you start with the one you've stored as the most frequently first-used object then search for the object that has the first-used object's ID stored on it to display next.



来源:https://stackoverflow.com/questions/11245135/algorithm-for-ordering-a-list-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!