input: Write a function to sort a list of objects based on one of their attributes. The function should take two parameters: the list of objects and the name of the attribute to sort by. The function should return the sorted list of objects. If the attribute does not exist in any of the objects, raise a ValueError.
  
  assert sort_by_attr([{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}], "age") == [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}]
  assert sort_by_attr([{"name": "Alice", "age": 25}, {"name": "Bob", "age": 21}], "age") == [{"name": "Bob", "age": 21}, {"name": "Alice", "age": 25}]
  assert sort_by_attr([{"name": "Alice", "age": 20}], "age") == [{"name": "Alice", "age": 20}]
  assert sort_by_attr([], "age") == []
  assert sort_by_attr([{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}, {"name": "Charlie", "age": 22}], "age") == [{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}, {"name": "Charlie", "age": 22}]
output: def sort_by_attr(objects, attr):
      for obj in objects:
          if attr not in obj:
              raise ValueError
      return sorted(objects, key=lambda x: x[attr])