Skip to main content
Question Most Common

In Appian, suppose you have a list of a!map objects with fields name and id. How can you sort this list alphabetically by the name field?

Answer

In Appian, we can use todatasubset() with a!pagingInfo() and a!sortInfo() to sort the list.


Appian Expression

a!localVariables(
local!data: {
a!map(id: 1, name: "Interface"),
a!map(id: 2, name: "Process Model"),
a!map(id: 3, name: "Records"),
a!map(id: 4, name: "Data type")
},

todatasubset(
local!data,
a!pagingInfo(
startIndex: 1,
batchSize: -1,
sort: a!sortInfo(
field: "name",
ascending: true()
)
)
).data
)


Step-by-step explanation


Step 1:

We have a list containing four maps. Each map has an id and a name.

ID

Name

1

Interface

2

Process Model

3

Records

4

Data type

The names are not in alphabetical order. The goal is to sort by name while each item keeps its own id, so the maps move as whole units.


Step 2: What does a!sortInfo() do?

a!sortInfo(
field: "name",
ascending: true()
)

This tells Appian which field to sort and in which direction.

  1. field: "name" means to compare the name values in each map.
  2. ascending: true() means to sort alphabetically from A to Z.

If you change it to ascending: false(), the names are sorted in reverse alphabetical order.


Step 3: What does a!pagingInfo() do?

a!pagingInfo(
startIndex: 1,
batchSize: -1,
sort: a!sortInfo(
field: "name",
ascending: true()
)
)

a!pagingInfo() specifies how many items to retrieve, where to start, and how to sort them.

  1. startIndex: 1 starts from the first item.
  2. batchSize: -1 requests all items rather than a limited batch.
  3. sort specifies the sorting rule we defined in Step 2.

Paging information is usually used to retrieve a specific page of data, but it can also configure sorting.


Step 4: What does todatasubset() do?

todatasubset(
local!data,
a!pagingInfo(...)
)

todatasubset() takes two inputs:

  1. local!data: the original list we want to sort.
  2. a!pagingInfo(): the instructions that tell Appian how to sort and retrieve the items.

Appian applies the sorting instructions and returns a DataSubset, which holds the resulting data along with paging information.


Step 5: Why do we use .data?

todatasubset(
local!data,
a!pagingInfo(...)
).data

todatasubset() returns a DataSubset, so .data is what extracts the sorted list of maps from it.


Output:

Image 69


Interested in more coding challenges ? visit : https://appianverse.com/coding

Try it yourself

Change the inputs or the expression and run it to see what it returns.

ri!data=

Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms

Check yourself

Question 1 of 3, on what the answer above covers.

  1. 1.
    You sort the maps in this question with a!sortInfo(field: "name") and leave out ascending. All four maps are returned. Which name comes first?
JuniorSeniorExpressions#coding#sailcoding