Reverse

Reverse( source )
Reverse( source )
Reverse( source )
Reverse( source )
reverse( source )
Reverse( source )

Description

The Reverse function returns in reverse order the items in source, which can be an Array, a Set, or a Page.

Parameters

Argument Type Definition and Requirements

source

The array, set, or page whose items should be reversed.

Returns

When source is an Array, an array containing the items found in source, with their original order reversed.

When source is a Set, a set containing the items found in source, with their original order reversed.

When source is a Page, a page of the items found in source, with their original order reversed.

Examples

  1. The following query passes an array to Reverse:

    Value result = await client.Query(
        Reverse(Arr("a", "b", "c", 1, 2, 3))
    );
    Arr(LongV(3), LongV(2), LongV(1), StringV(c), StringV(b), StringV(a))
    result, err := client.Query(
    	f.Reverse(f.Arr{"a", "b", "c", 1, 2, 3}))
    
    if (err != nil) {
    	fmt.Println(err)
    } else {
    	fmt.Println(result)
    }
    Arr{3, 2, 1, "c", "b", "a"}
    System.out.println(
        client.query(
            Reverse(
                Arr(
                    Value("a"), Value("b"), Value("c"),
                    Value(1), Value(2), Value(3)
                )
            )
        ).get());
    [3, 2, 1, "c", "b", "a"]
    client.query(
      q.Reverse(["a", "b", "c", 1, 2, 3])
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.log('Error:', err))
    [ 3, 2, 1, 'c', 'b', 'a' ]
    print(client.query(
      q.reverse(["a", "b", "c", 1, 2, 3])
    ))
    [3, 2, 1, 'c', 'b', 'a']
    println(Await.result(
        client.query(
            Reverse(Arr("a", "b", "c", 1, 2, 3))
        ),
        5.seconds
    ))
    [3, 2, 1, "c", "b", "a"]
  2. The following query reverses the set of documents in the Letters collection (established in the Indexing tutorial, and then paginates the set:

    Value result = await client.Query(
        Paginate(Reverse(Documents(Collection("Letters"))))
    );
    ObjectV(data: Arr(RefV(id = "126", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "125", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "124", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "123", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "122", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "121", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "120", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "119", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "118", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "117", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "116", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "115", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "114", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "113", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "112", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "111", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "110", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "109", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "108", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "107", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "106", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "105", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "104", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "103", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "102", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "101", collection = RefV(id = "Letters", collection = RefV(id = "collections")))))
    result, err := client.Query(
    	f.Paginate(f.Reverse(f.Documents(f.Collection("Letters")))))
    
    if (err != nil) {
    	fmt.Println(err)
    } else {
    	fmt.Println(result)
    }
    Obj{"data": Arr{RefV{ID: "126", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "125", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "124", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "123", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "122", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "121", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "120", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "119", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "118", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "117", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "116", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "115", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "114", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "113", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "112", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "111", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "110", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "109", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "108", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "107", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "106", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "105", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "104", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "103", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "102", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "101", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}}}
    System.out.println(
        client.query(
            Paginate(Reverse(Documents(Collection("Letters"))))
        ).get());
    {data: [ref(id = "126", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "125", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "124", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "123", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "122", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "121", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "120", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "119", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "118", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "117", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "116", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "115", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "114", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "113", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "112", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "111", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "110", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "109", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "108", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "107", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "106", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "105", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "104", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "103", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "102", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "101", collection = ref(id = "Letters", collection = ref(id = "collections")))]}
    client.query(
      q.Paginate(q.Reverse(q.Documents(q.Collection("Letters"))))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.log('Error:', err))
    {
      data: [
        Ref(Collection("Letters"), "126"),
        Ref(Collection("Letters"), "125"),
        Ref(Collection("Letters"), "124"),
        Ref(Collection("Letters"), "123"),
        Ref(Collection("Letters"), "122"),
        Ref(Collection("Letters"), "121"),
        Ref(Collection("Letters"), "120"),
        Ref(Collection("Letters"), "119"),
        Ref(Collection("Letters"), "118"),
        Ref(Collection("Letters"), "117"),
        Ref(Collection("Letters"), "116"),
        Ref(Collection("Letters"), "115"),
        Ref(Collection("Letters"), "114"),
        Ref(Collection("Letters"), "113"),
        Ref(Collection("Letters"), "112"),
        Ref(Collection("Letters"), "111"),
        Ref(Collection("Letters"), "110"),
        Ref(Collection("Letters"), "109"),
        Ref(Collection("Letters"), "108"),
        Ref(Collection("Letters"), "107"),
        Ref(Collection("Letters"), "106"),
        Ref(Collection("Letters"), "105"),
        Ref(Collection("Letters"), "104"),
        Ref(Collection("Letters"), "103"),
        Ref(Collection("Letters"), "102"),
        Ref(Collection("Letters"), "101")
      ]
    }
    print(client.query(
      q.paginate(q.reverse(q.documents(q.collection("Letters"))))
    ))
    {'data': [Ref(id=126, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=125, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=124, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=123, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=122, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=121, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=120, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=119, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=118, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=117, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=116, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=115, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=114, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=113, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=112, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=111, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=110, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=109, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=108, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=107, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=106, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=105, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=104, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=103, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=102, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=101, collection=Ref(id=Letters, collection=Ref(id=collections)))]}
    println(Await.result(
        client.query(
            Paginate(Reverse(Documents(Collection("Letters"))))
        ),
        5.seconds
    ))
    {data: [ref(id = "126", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "125", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "124", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "123", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "122", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "121", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "120", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "119", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "118", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "117", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "116", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "115", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "114", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "113", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "112", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "111", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "110", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "109", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "108", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "107", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "106", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "105", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "104", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "103", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "102", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "101", collection = ref(id = "Letters", collection = ref(id = "collections")))]}
  3. The following query reverses the page of results from paginating the documents in the Letters collection:

    Value result = await client.Query(
        Reverse(
            Paginate(
                Documents(Collection("Letters")),
                after: Ref(Collection("Letters"), 104),
                size: 3
            )
        )
    );
    ObjectV(before: Arr(RefV(id = "104", collection = RefV(id = "Letters", collection = RefV(id = "collections")))),after: Arr(RefV(id = "107", collection = RefV(id = "Letters", collection = RefV(id = "collections")))),data: Arr(RefV(id = "106", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "105", collection = RefV(id = "Letters", collection = RefV(id = "collections"))), RefV(id = "104", collection = RefV(id = "Letters", collection = RefV(id = "collections")))))
    result, err := client.Query(
    	f.Reverse(
    		f.Paginate(
    			f.Documents(f.Collection("Letters")),
    			f.Size(3),
    			f.After(f.RefCollection(f.Collection("Letters"), 104)))))
    
    if (err != nil) {
    	fmt.Println(err)
    } else {
    	fmt.Println(result)
    }
    Obj{"after": Arr{RefV{ID: "107", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}}, "data": Arr{RefV{ID: "106", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "105", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}, RefV{ID: "104", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}}, "before": Arr{RefV{ID: "104", Collection: &RefV{ID: "Letters", Collection: &RefV{ID: "collections"}}}}}
    System.out.println(
        client.query(
            Reverse(
                Paginate(
                    Documents(Collection("Letters"))
                )
                .size(3)
                .after(Ref(Collection("Letters"), "104"))
            )
        ).get());
    {before: [ref(id = "104", collection = ref(id = "Letters", collection = ref(id = "collections")))], after: [ref(id = "107", collection = ref(id = "Letters", collection = ref(id = "collections")))], data: [ref(id = "106", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "105", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "104", collection = ref(id = "Letters", collection = ref(id = "collections")))]}
    client.query(
      q.Reverse(
        q.Paginate(
          q.Documents(q.Collection("Letters")),
          { size: 3, after: q.Ref(q.Collection("Letters"), "104") }
        )
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.log('Error:', err))
    {
      before: [ Ref(Collection("Letters"), "104") ],
      after: [ Ref(Collection("Letters"), "107") ],
      data: [
        Ref(Collection("Letters"), "106"),
        Ref(Collection("Letters"), "105"),
        Ref(Collection("Letters"), "104")
      ]
    }
    print(client.query(
      q.reverse(
        q.paginate(
          q.documents(q.collection("Letters")),
          size = 3,
          after = q.ref(q.collection("Letters"), 104)
        )
      )
    ))
    {'before': [Ref(id=104, collection=Ref(id=Letters, collection=Ref(id=collections)))], 'after': [Ref(id=107, collection=Ref(id=Letters, collection=Ref(id=collections)))], 'data': [Ref(id=106, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=105, collection=Ref(id=Letters, collection=Ref(id=collections))), Ref(id=104, collection=Ref(id=Letters, collection=Ref(id=collections)))]}
    println(Await.result(
        client.query(
            Reverse(
                Paginate(
                    Documents(Collection("Letters")),
                    size = 3,
                    cursor = After(Ref(Collection("Letters"), "104"))
                )
            )
        ),
        5.seconds
    ))
    {before: [ref(id = "104", collection = ref(id = "Letters", collection = ref(id = "collections")))], after: [ref(id = "107", collection = ref(id = "Letters", collection = ref(id = "collections")))], data: [ref(id = "106", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "105", collection = ref(id = "Letters", collection = ref(id = "collections"))), ref(id = "104", collection = ref(id = "Letters", collection = ref(id = "collections")))]}

    Notice that Reverse has reversed the order of the results in the page, but has not affected the pagination order.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!