package secgroup

import "sort"

// SortByCreatedAt sorts ListSecurityGroups by the CreatedAt field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lsg ListSecurityGroups) SortByCreatedAt(inverse bool) ListSecurityGroups {
	if len(lsg.Data) < 2 {
		return lsg
	}

	sort.Slice(lsg.Data, func(i, j int) bool {
		if inverse {
			return lsg.Data[i].CreatedAt > lsg.Data[j].CreatedAt
		}

		return lsg.Data[i].CreatedAt < lsg.Data[j].CreatedAt
	})

	return lsg
}

// SortByUpdatedAt sorts ListSecurityGroups by the UpdatedAt field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lsg ListSecurityGroups) SortByUpdatedAt(inverse bool) ListSecurityGroups {
	if len(lsg.Data) < 2 {
		return lsg
	}

	sort.Slice(lsg.Data, func(i, j int) bool {
		if inverse {
			return lsg.Data[i].UpdatedAt > lsg.Data[j].UpdatedAt
		}

		return lsg.Data[i].UpdatedAt < lsg.Data[j].UpdatedAt
	})

	return lsg
}
