3

According to this document, the type type[] is "An array of items of a defined type" and the type List is "An ordered collection of items".

I've tested the output of both structures (with integer values) and they are the same. I mean, the items of the List were not ordered.

So my question is: what exactly is the difference between these two types?

4
  • How did you verify it not to be ordered? Did you access data using their index?
    – Jayant Das
    Sep 26, 2018 at 11:53
  • I've printed it in the page using aura:iteration. <h4>Array</h4> <ul> <aura:iteration items="{!v.attrArray}" var="n"> <li>{!n}</li> </aura:iteration> </ul> <h4>List</h4> <ul> <aura:iteration items="{!v.attrList}" var="n"> <li>{!n}</li> </aura:iteration> </ul>
    – Saulo
    Sep 26, 2018 at 12:03
  • So what did you observe - did they not appear in the same order as how you added those in the array or list?
    – Jayant Das
    Sep 26, 2018 at 12:43
  • They appear in the same order. I've initialised both attributes with the array [5, 4, 3, 2, 1]. Notice I've specified the array, for both attributes, in a decrescent order. Once documentation says List type is "an ordered collection of items", I was expecting attrList attribute array to be printed in ascendent order.
    – Saulo
    Sep 26, 2018 at 13:15

2 Answers 2

1

An "ordered" object means the values will appear in the same order each time you access the variable. This is in contrast to an "unordered" object, which may return its values in different orders each time you use them. For example, an Object can return an unordered list of keys. There is no guarantee as to the order the keys will appear from time to time.

In the strictest sense, List and Array are exactly the same. However, we are encouraged to use the case-sensitive Apex version of an object. What this means is that you should define an attribute as a List and not an Array. This is simply how Lightning (or, more specifically, Aura, the underlying runtime) has been designed. Similarly, it's recommended that you use a Map instead of an Object to avoid potential side effects.

0

Based on your comment:

They appear in the same order. I've initialised both attributes with the array [5, 4, 3, 2, 1]. Notice I've specified the array, for both attributes, in a decrescent order. Once documentation says List type is "an ordered collection of items", I was expecting attrList attribute array to be printed in ascendent order.

I assume you are expecting that you added items in descending order and that with "ordered" you are expecting those to be printed in ascending order. That's not what is meant by an ordered collection. The behavior you observe is the expected behavior. You are getting confused with ordered vs. sorted here. You will get that output as you expect only when you sort your list/array which by default is not sorted but only ordered.

An ordered collection means that the collection maintains the "order" of the items being added into that collection and that if you access a collection (list or array) with an index, you are guaranteed to get the same value every time on that index (unless you change the value).

You can find more details on these links:

And for your question:

So my question is: what exactly is the difference between these two types?

You will find the exact difference between a List and Array when you refer to the documentation links provided above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.