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.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    [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.error('Error: %s', err))
    [ 3, 2, 1, 'c', 'b', 'a' ]
    result = client.query(
      q.reverse(["a", "b", "c", 1, 2, 3])
    )
    print(result)
    [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.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[data:[{126 0xc0001321b0 0xc0001321b0 <nil>} {125 0xc000132360 0xc000132360 <nil>} {124 0xc000132510 0xc000132510 <nil>} {123 0xc0001326c0 0xc0001326c0 <nil>} {122 0xc000132870 0xc000132870 <nil>} {121 0xc000132a20 0xc000132a20 <nil>} {120 0xc000132bd0 0xc000132bd0 <nil>} {119 0xc000132d80 0xc000132d80 <nil>} {118 0xc000132f30 0xc000132f30 <nil>} {117 0xc0001330e0 0xc0001330e0 <nil>} {116 0xc000133290 0xc000133290 <nil>} {115 0xc000133440 0xc000133440 <nil>} {114 0xc0001335f0 0xc0001335f0 <nil>} {113 0xc0001337a0 0xc0001337a0 <nil>} {112 0xc000133950 0xc000133950 <nil>} {111 0xc000133b00 0xc000133b00 <nil>} {110 0xc000133cb0 0xc000133cb0 <nil>} {109 0xc000133e60 0xc000133e60 <nil>} {108 0xc000154030 0xc000154030 <nil>} {107 0xc00016c0c0 0xc00016c0c0 <nil>} {106 0xc00016c270 0xc00016c270 <nil>} {105 0xc00016c420 0xc00016c420 <nil>} {104 0xc00016c5d0 0xc00016c5d0 <nil>} {103 0xc00016c780 0xc00016c780 <nil>} {102 0xc00016c930 0xc00016c930 <nil>} {101 0xc00016cae0 0xc00016cae0 <nil>}]]
    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.error('Error: %s', 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")
      ]
    }
    result = client.query(
      q.paginate(q.reverse(q.documents(q.collection("Letters"))))
    )
    print(result)
    {'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.Ref(f.Collection("Letters"), 104)))))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[after:[{107 0xc00016e360 0xc00016e360 <nil>}] before:[{104 0xc00016e1b0 0xc00016e1b0 <nil>}] data:[{106 0xc00016e510 0xc00016e510 <nil>} {105 0xc00016e6c0 0xc00016e6c0 <nil>} {104 0xc0001480f0 0xc0001480f0 <nil>}]]
    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.error('Error: %s', err))
    {
      before: [ Ref(Collection("Letters"), "104") ],
      after: [ Ref(Collection("Letters"), "107") ],
      data: [
        Ref(Collection("Letters"), "106"),
        Ref(Collection("Letters"), "105"),
        Ref(Collection("Letters"), "104")
      ]
    }
    result = client.query(
      q.reverse(
        q.paginate(
          q.documents(q.collection("Letters")),
          size=3,
          after=q.ref(q.collection("Letters"), 104),
        )
      )
    )
    print(result)
    {'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.

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!