group_by_window unexpectedly uses key for ordering group output
@marcmk6,

Groups windows of elements by key and reduces them.

This transformation maps each consecutive element in a dataset to a key using `key_func` and groups the elements by key. It then applies reduce_func to at most `window_size_func(key)` elements matching the same key. All except the final window for each key will contain **window_size_func(key)** elements; the final window may be smaller.

You may provide either a constant **window_size** or a window size determined by the key through **window_size_func**

https://www.tensorflow.org/api_docs/python/tf/data/Dataset#group_by_window

Thank you! Hi @tilakrayal,

I've already read the documentation before making this post and I know what this function is used for. 
The thing that seems problematic to me is it uses the **values** of the key to sort the outputs, in an ascending order. In the first example I provided, the key equals the element. You can see in the output that although the elements are grouped together, the groups are also sorted by the values of their respective keys. If I switch to use the negative value of the element as key, the output order is reversed. 

In my opinion the output order should not be related to the values of the key. This binding cancels all the prior randomness of elements and this sorting behavior is not documented. I think the most natural implementation is that the output ordering being identical to the ordering before grouping. 
For instance, let's say the elements before grouping is `[8, 7, 7, 6, 3, 2, 8, 3, 8, 4, 9, 4, 6, 8, 6, 5, 5, 9, 7, 7, 9, 2, 7, 5, 9, 7, 9, 1, 6, 9, 9, 8, 5, 6, 3, 7, 8, 9, 8, 6, 4, 5, 4, 9, 8]`. The output in this case should be `[8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 3, 3, 3, 2, 2, 4, 4, 4, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, 1]`.

Thanks @marcmk6,
**group_by_window** does not iterate through the entire dataset before outputing something. It iterates through enough of the dataset for one of the windows to reach window_size. 

Alos group_by_window doesn't explicitly sort the elements within each group based on their keys. It groups the  elements based on the key and then applies the reduce function to the elements within each group.

If you need the elements within each group to be sorted based on their keys, you might need to perform additional sorting after applying **group_by_window**. This could be done by using the **[tf.data.Dataset.map](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map)** transformation along with a custom sorting function. Thank you! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61515">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61515">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

binary

### TensorFlow version

tf-nightly 2.15.0.dev20230810

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

`tf.data.Dataset.group_by_window` uses the values of key for ordering outputs, which is neither documented nor desired.
This behavior cancels all the prior randomness and links groups/outputs order with the order of the key.
My expectation is that the key is used to group elements only and does nothing else. 

### Standalone code to reproduce the issue

```python
import tensorflow as tf

datasets = tf.data.Dataset.from_tensor_slices([
    tf.data.Dataset.from_tensors(i).repeat(i)
    for i in range(1, 10)
])

datasets = datasets.interleave(
    map_func=lambda x: x,
    cycle_length=1,
    block_length=1,
)

shuffled_ds = datasets.shuffle(100)
print('Shuffled dataset:', [e.numpy() for e in shuffled_ds])

ds = (
    shuffled_ds
    .group_by_window(
        key_func=lambda x: tf.cast(x, tf.int64),
        reduce_func=lambda key, dataset: dataset,
        window_size=10,
    )
)

ds2 = (
    shuffled_ds
    .group_by_window(
        key_func=lambda x: -tf.cast(x, tf.int64),
        reduce_func=lambda key, dataset: dataset,
        window_size=10,
    )
)

print('Output re-ordered by `group_by_window` using key')
print([e.numpy() for e in ds])
print('-'*50)
print([e.numpy() for e in ds2])
```


### Relevant log output

```python
Shuffled dataset: [8, 7, 7, 6, 3, 2, 8, 3, 8, 4, 9, 4, 6, 8, 6, 5, 5, 9, 7, 7, 9, 2, 7, 5, 9, 7, 9, 1, 6, 9, 9, 8, 5, 6, 3, 7, 8, 9, 8, 6, 4, 5, 4, 9, 8]
Output re-ordered by `group_by_window` using key
[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9]
--------------------------------------------------
[9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1]
```

