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

Parameter Type Definition and Requirements

source

Array, Set, or Page

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:

    try
    {
        Value result = await client.Query(
            Reverse(Arr("a", "b", "c", 1, 2, 3))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    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] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    [ 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']
    Reverse(['a', 'b', 'c', 1, 2, 3])
    [ 3, 2, 1, 'c', 'b', 'a' ]
    Query metrics:
    •    bytesIn:  31

    •   bytesOut:  32

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 1ms

    •    retries:   0

  2. The following query reverses the set of documents in the Letters collection (established in the Indexing tutorial, and then paginates the set:

    try
    {
        Value result = await client.Query(
            Paginate(Reverse(Documents(Collection("Letters"))))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    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] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      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)))]}
    Paginate(Reverse(Documents(Collection('Letters'))))
    {
      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")
      ]
    }
    Query metrics:
    •    bytesIn:    63

    •   bytesOut: 2,727

    • computeOps:     1

    •    readOps:     8

    •   writeOps:     0

    •  readBytes: 1,300

    • writeBytes:     0

    •  queryTime:  13ms

    •    retries:     0

  3. The following query reverses the page of results from paginating the documents in the Letters collection:

    try
    {
        Value result = await client.Query(
            Reverse(
                Paginate(
                    Documents(Collection("Letters")),
                    after: Ref(Collection("Letters"), 104),
                    size: 3
                )
            )
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    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] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      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)))]}
    Reverse(
      Paginate(
        Documents(Collection('Letters')),
        { size: 3, after: Ref(Collection('Letters'), '104') }
      )
    )
    {
      before: [ Ref(Collection("Letters"), "104") ],
      after: [ Ref(Collection("Letters"), "107") ],
      data: [
        Ref(Collection("Letters"), "106"),
        Ref(Collection("Letters"), "105"),
        Ref(Collection("Letters"), "104")
      ]
    }
    Query metrics:
    •    bytesIn:   124

    •   bytesOut:   564

    • computeOps:     1

    •    readOps:     8

    •   writeOps:     0

    •  readBytes: 1,174

    • writeBytes:     0

    •  queryTime:  14ms

    •    retries:     0

    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!