sortlist: Sort List Entries Alphabetically

View source: R/sortlist.R

sortlistR Documentation

Sort List Entries Alphabetically

Description

This function sorts the entries of a structured list in alphabetical order, including all nested sublists recursively, making it easier to navigate nested list structures and compare different lists. Unnammed items are listed last. Attributes of the original list are preserved.

Usage

sortlist(lst, recursive = TRUE, convert.data.frames = FALSE)

Arguments

lst

list to be sorted.

recursive

logical flag indicating whether to apply sorting recursively to all sublists. Defaults to TRUE. If FALSE, only the top-level entries in the list will be sorted.

convert.data.frames

logical flag. If TRUE, data frames are converted to lists.

Value

Structured list with alphabetically sorted entries. If recursive = TRUE, all sublists are also sorted alphabetically. If the list contains unnamed elements, they are left unsorted.

Examples

# Create a nested list
my_list = list(
b = list(d = 4, a = 2),
empty = list(),
'unnamed',
a = 1,
f = list(b = 3, a = list(d = seq(5), 'unnamed', c = 2))
)

# Sort the list recursively
sorted_list_recursive = sortlist(my_list)
print(sorted_list_recursive)


obreschkow/cooltools documentation built on Nov. 16, 2024, 2:46 a.m.