Hi,<br><br>I have a list of entities.&nbsp; Each entity has a priority field and an order field.&nbsp; The priority field contain the input values to my algorithm, the order fields contain the output value of my algorithm.&nbsp; My algorithm needs to assign 1 to the order field of the entity with the lowest priority value, 2 to the order field of the entity with the next lowest priority value, .... and so on.&nbsp; I've written a function orderByPriority that does that.&nbsp; However, it also does something else that is undesireable.&nbsp; It reorders the list.
<br><br>My question is, how do I preserve the ordering of entities in my list and still be able to assign the proper order values to each entity?&nbsp; Is there an efficient way to do this?&nbsp; How else might I improve my orderByPriority algorithm.
<br><br>Thanks<br><br>-John<br><br>&gt; import Data.List<br><br>&gt; data Entity = Entity {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; entityId :: Int,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; priority :: Float,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; order :: Int<br>&gt;&nbsp;&nbsp; }<br><br>&gt; initEntity = Entity {
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; entityId = 0,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; priority = 0.0,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; order = 0<br>&gt;&nbsp;&nbsp; }<br><br>&gt; myList = [<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 1, priority = 8.7 },<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 2, priority = 5.4
 },<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 3, priority = 2.9 },<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 4, priority = 5.4 },<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 5, priority = 1.3 },<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 6, priority = 
3.5 },<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; initEntity { entityId = 7, priority = 9.5 }<br>&gt;&nbsp;&nbsp; ]<br><br>&gt; comparePriority entity1 entity2 =<br>&gt;&nbsp;&nbsp; compare (priority entity1) (priority entity2)<br><br>&gt; orderByPriority :: [Entity] -&gt; [Entity]
<br>&gt; orderByPriority entities = assignOrder orderList 1<br>&gt;&nbsp;&nbsp; where<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; orderList = sortBy comparePriority entities<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; assignOrder [] _ = []<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; assignOrder (entity:entities) count =<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (entity { order = count }):(assignOrder entities (count + 1))
<br><br>&gt; instance Show Entity where<br>&gt;&nbsp;&nbsp; show entity = &quot;{&quot; ++<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &quot;entityId:&quot; ++ (show $ entityId entity) ++ &quot;,&quot; ++<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &quot;priority:&quot; ++ (show $ priority entity) ++ &quot;,&quot; ++
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &quot;order: &quot; ++ (show $ order entity) ++<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &quot;}&quot;<br><br>