I would advise against storing the item itself in model.itemActive (Option 
1). This would be storing the same data in 2 places, which is not good.
You really need 2 elements:

   1. A key to identify the item: either A: the index of the item in the 
   collection or B: a separate key that you store in the item
   2. A key to sort the item: either A: the index of the item in the 
   collection (i.e. the collection is always in the right order) or B: a 
   separate sortKey stored in the item
   
With your Option 2, it is not possible to make the model.itemActive = index 
(1A) AND keep the list in the right order (2A) 
Because you need to update the itemActive every time you resort the List.
To do this, you need some value to find back the active item in the 
resorted List. And for this, something needs to be a separate 
identification key in the item.

The same applies to the Array and Dict Options. If you keep the collection 
(Array or Dict) always in right order, where do you store the 
identification key?

Whichever option you choose, you either have expensive sorts (Dict and 
Array options), or you have expensive get/sets (List options)

In front-end, which frequent adds/ removes/ sorts etc, I typically go with 
a List-type option.

type alias Model =
  { collection : List Item
  , itemActive : ID
  }

type alias Item = 
  { item : String
  , id : ID
  }


type alias ID = Int


getItem : List Item -> ID -> Maybe Item
getItem : list id =
  List.filter (\item -> item.id == id) list

PS: Option 4 is titled "array with index" but it the code in fact "array 
with explicit item".
(You probably meant the code to be with index. Array with explicit item 
would not make much sense, because you lose the fast index-based get and 
set on arrays)

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to